https://kotlinlang.org logo
Title
h

Hullaballoonatic

06/02/2019, 5:43 PM
why can't functions defined as such:
fun foo() = true
be referenced as such:
val bar: () -> Boolean = foo
? or conversely:
interface Foo {
    val bar: () -> Boolean
}

object Bar : Foo {
    override fun bar() = true // nope. why not?
    override val bar = { true } // yep
}
d

Dias

06/02/2019, 5:49 PM
because
( )->Boolean
is function that returns boolean while
true
IS boolean
a

Adam Powell

06/02/2019, 5:51 PM
one is a function and the other is a property holding a function reference
h

Hullaballoonatic

06/02/2019, 5:52 PM
i thought all functions WERE properties in the jvm?
a

Adam Powell

06/02/2019, 5:58 PM
not in a way that is significant here
kotlin properties are implemented as jvm methods generally, so more like the other way around for this case
you can get a function reference using
::
if you need one
h

Hullaballoonatic

06/02/2019, 6:01 PM
righto, thanks for reminding me of that
👍 1
a

Adam Powell

06/02/2019, 6:03 PM
also a common gotcha in this area is that
foo::bar != foo::bar
- they're different instances
so be careful if you're trying to
collectionOfFunctionReferences.remove(foo::bar)