As @darksnake asked here, I support my case with some examples. The code is taken from there
interface Swimable {
fun swim()
}
class SwimableImpl : Swimable {
override fun swim() {
println("swim!")
}
}
interface Jumpable {
fun jump()
}
class JumpableImpl : Jumpable {
override fun jump() {
println("jump!")
}
}
class Frog(
swimable: Swimable = SwimableImpl(),
jumpable: Jumpable = JumpableImpl()
) : Swimable by swimable, Jumpable by jumpable
fun main() {
val frog = Frog()
frog.jump() // jump!
frog.swim() // swim!
}
The question is: how to use parameters of the base class inside the compositions without callbacks or proxifying them inside the every method?
class JumpableImpl : Jumpable {
override fun jumpHigh() {
stamina -= height() // How???
}
override fun jumpLow() { // This method should be dirty
stamina -= height() / 2 // How???
}
}
class Frog(
swimable: Swimable = SwimableImpl(),
jumpable: Jumpable = JumpableImpl()
) : Swimable by swimable, Jumpable by jumpable {
var age = 42
var stamina = 128
private set
private fun height() = age * 2
}