https://kotlinlang.org logo
Title
g

ghedeon

12/19/2018, 9:07 PM
An example with
::
(bound callable reference?). Any way to properly import
Companion
so I don't see it in the code?
class Foo(bar: () -> Bar)

class Bar {
    companion object {
        fun newInstance() = Bar()
    }
}
....
Foo(Bar.Companion::newInstance) //<--- works
Foo(Bar::newInstance) //<--- doesn't work
r

Ruckus

12/19/2018, 10:03 PM
No, as those have different meaning.
Bar::newInstance
is a reference to a function on
Bar
.
You could do this (though it feels a bit of a hack and changes the semantics considerably):
class Bar {
    companion object : () -> Bar {
        override fun invoke() = Bar()
    }
}

Foo(Bar)
:tnx: 1
g

ghedeon

12/20/2018, 7:06 AM
probably won't use it but I'm glad I've seen it)