Is there a shorthand / concise way to easily deleg...
# getting-started
a
Is there a shorthand / concise way to easily delegate a function to another object’s function, like
Copy code
class Foo(
    val bar:Bar
) {
    fun frob(x:Int, y:Int, z:Int) by bar
}
… assuming
Bar
has a method of the exact signature. I am aware of class based delegation but I don’t want
Foo
to implement
Bar
, or even
by bar::frob
c
Copy code
fun frob(x: Int, y: Int, z: Int) = bar.frob(x, y, z)
a
Sure, but as the number of parameters and/or the number of delegates that are needed this grows cumbersome. Thanks
c
Yeah. I'm not aware of any simpler syntax.
h
I don't like this solution (you may not even consider it a solution as you ask to delegate a function):
Copy code
class Bar {
    fun frob(x: Int, y: Int, z: Int) = x + y + z
}

class Foo(
	val bar: Bar
) {
    val frob = bar::frob
}

fun main() {
    val bar = Bar()
    val foo = Foo(bar)
    val ans = foo.frob(1, 2, 4)
    println(ans)
}
Won't work when you need to override an interface with method
frob