https://kotlinlang.org logo
#announcements
Title
# announcements
i

Ivan Pavlov

11/27/2020, 11:38 AM
Hi. Does anyone know why delegation for
fun
interface with lambdas (
class D
) doesn't work? I'd really want to use delegation that way, not like
class C
Copy code
fun interface A {
    fun f()
}

fun interface B {
    fun f()
}

class C(b: B) : A by A(b::f) //works

class D(b: B) : A by { b.f() } //doesn't work
v

Vampire

11/27/2020, 2:27 PM
Copy code
class D(b: B) : A by { b.f() } as A
e

ephemient

11/27/2020, 3:00 PM
Copy code
class D(b: B) : A by (A { b.f() })
i

Ivan Pavlov

11/27/2020, 3:04 PM
@Vampire thats even worse, in your example if I remove
fun
for
interface A
it still compiles which is not a good option I think
😞 1
@ephemient this options is just another way to do like in
class C
. I can also use
by A({ b.f() })
but my question is why
{ b.f() }
doesn't work? I mean, it's intentional behavior or it's just compiler not as smart as I want? 🙂
e

ephemient

11/27/2020, 3:10 PM
type inference isn't smart enough, it seems. I think it's still better than C, as you can put arbitrary code in there beyond just a function reference
4 Views