dave08
08/30/2023, 1:54 PMfun interface Foo
and I have class Bar(val foo: Foo)
I can still create Bar
using a lambda that doesn't implement the fun interface... but when I try to do fun provideFoo(): Foo = { ... }
, it doesn't let me... why not? What's the difference? Can I maybe cast it to work somehow without wrapping it in a Foo { { }() }
?Sam
08/30/2023, 1:58 PMephemient
08/30/2023, 1:59 PMusing a lambda that doesn't implement the fun interface
that's not true, the lambda in that context does implement the fun interface. if you write
val foo: (...) -> ... = { ... }
there is no automatic conversation that lets you write
Bar(foo)
ephemient
08/30/2023, 2:01 PMdave08
08/30/2023, 2:02 PMval someLambda = { ... }
fun provideFoo(): Foo = Foo(someLambda)
I wonder if that syntax is documented somewhere...ephemient
08/30/2023, 2:02 PMBar { ... }
is really
Bar(Foo { ... })
ephemient
08/30/2023, 2:02 PMephemient
08/30/2023, 2:03 PMdave08
08/30/2023, 2:03 PMdave08
08/30/2023, 2:04 PMdave08
08/30/2023, 2:05 PMdave08
08/30/2023, 2:07 PM::Printer
does... you're calling a function reference on the actual fun interface
...?dave08
08/30/2023, 2:08 PMaddPrinter
method?ephemient
08/30/2023, 2:09 PMJavier
08/30/2023, 2:49 PMdave08
08/30/2023, 3:21 PM::Printer
is instantiating, it's just an interface w/o an implementation?ephemient
08/30/2023, 5:11 PM(() -> Unit) -> Printer
"constructor" which you used to need to write by hand but is now provided by the Kotlin compiler