say I have two functions with the same signature, ...
# announcements
r
say I have two functions with the same signature, is it possible to somehow delegate to the other without the need to specify the arguments?
Copy code
fun foo(i: Int): String = ...
fun bar(i: Int): String by foo // does not work, is there another syntax?
m
Copy code
fun foo(i: Int) = i * i

val bar = ::foo

fun main() {
  println(bar(3))
}
This assigns a function reference to
bar
, which you can then use as a function.
r
good hint but not exactly what I need though. It has to be a function as it is declared this way in the interface
t
Why?
t
Why not just
fun bar(i: Int) = foo(i)
?
r
because
is it possible to somehow delegate to the other without the need to specify the arguments?
t
So why doesn't
val bar = ::foo
work for you?
That makes
bar
the same function as
foo
.
r
as I wrote, it has to fulfil the definition of an interface, for instance:
Copy code
interface A{
    fun bar(i: Int)
}
class B: A{
    fun foo(i: Int) = TODO()
    override val bar = ::foo  // does not override something
}
t
Ahhh, I see.