samedi 12 février 2022

Useless Kotlin delegate

I'm trying to understand why we have feature as delegate in Kotlin. This example is similar to wiki example https://en.wikipedia.org/wiki/Delegation_pattern

Check this commented fragment, I don't need it for using area function on bounds so why we should use : ClosedShape by bounds ?

interface ClosedShape {
    fun area(): Int
}

class Rectangle(val width: Int, val height: Int) : ClosedShape {
    override fun area() = width * height
}

class Window(private val bounds: ClosedShape)  /*: ClosedShape by bounds */ {
    init {
        bounds.area()
    }
}

Aucun commentaire:

Enregistrer un commentaire