mandeep
08/04/2020, 8:25 PMList
of some type. For example:
interface Base {
fun print()
}
class BaseImpl(val x: Int) : Base {
override fun print() { print(x) }
}
class Derived(val b: List<Base>) : Base by b
// or maybe use varargs
class Derived(vararg val b: Base) : Base by b
The Derived
class would just iterate over the list and invoke whatever is being overridden.
This is really useful in scenarios when you can only register a single instance as a callback to somethinggildor
08/05/2020, 4:12 AMclass IterableBase(val instances: List<Base>) : Base {
override fun print() { instances.forEach( print() }
}
class Derived(val b: List<Base>) : Base by IterableBase(b)
gildor
08/05/2020, 4:13 AMdiesieben07
08/05/2020, 7:44 AMUnit
mandeep
08/05/2020, 7:41 PMgildor
08/05/2020, 11:37 PMmandeep
08/06/2020, 2:06 AMUnit
. For all others you have to manually implement the method. So:
interface Base {
fun print()
fun foo(): String
}
class BaseImpl1(val x: Int) : Base {
override fun print() { print(x) }
override fun foo() = "1"
}
class BaseImpl2(val x: Int) : Base {
override fun print() { print(x) }
override fun foo() = "2"
}
class Derived(vararg val delegates: Base) : Base by delegates {
override fun foo(): String {
// Manually implement some logic
return delegates.map { it.foo() }.joinToString()
}
}
gildor
08/06/2020, 2:52 AMmandeep
08/06/2020, 7:05 PMinterface
implementations on a single classgildor
08/06/2020, 11:37 PM