https://kotlinlang.org logo
j

Jukka Siivonen

08/24/2020, 9:18 AM
If I have a function with parameter param: () -> Boolean how can I call this function using method reference Foo:myFunction as a parameter?
d

diesieben07

08/24/2020, 9:20 AM
What's
Foo
here? A class?
j

Jukka Siivonen

08/24/2020, 9:20 AM
Good question, interface actually
d

diesieben07

08/24/2020, 9:21 AM
Then your
param
either needs an instance parameter or you need to do
someFooInstance::myFunction
j

Jukka Siivonen

08/24/2020, 9:22 AM
The actual use case is that I need to call this function in two places, other just () -> true and other using Foo:myFunction
d

diesieben07

08/24/2020, 9:23 AM
Well
Foo::myFunction
is of type
Foo.() -> Boolean
, it needs the actual instance of
Foo
to call the method on.
If you want
() -> Boolean
you need to pre-bind the receiver:
myInstance::myFunction
j

Jukka Siivonen

08/24/2020, 9:26 AM
ah, correct, forgot about that.
3 Views