https://kotlinlang.org logo
#getting-started
Title
# getting-started
g

Gilles Barbier

09/19/2021, 9:09 PM
Hello, I've spent so much time without managing to get any response: Given a
Kfunction
obtained from an expression such as
obj::method
where obj is an instance from a Class. How can I retrieve the underlying
obj
from the
Kfunction
???
s

smit01

09/19/2021, 9:44 PM
class Foo{ fun foo()=println("foo") } val obj:Foo = Foo() val action:()->Unit = obj::foo action.invoke() action() output :- foo foo "If you could elaborate more on which object you want to retrieve and from what"??
Tyep of action KFunction0<Unit>
g

Gilles Barbier

09/19/2021, 9:47 PM
yes, my question is: how do I retrieve obj from action?
If I know only
action
, is there a way to access
obj
?
Internally, obj is stored inside action - it's necessary to invoke it, but I can not find a way to access it
k

Kirill Grouchnikov

09/19/2021, 10:36 PM
KFunction doesn't need to be defined on an object. What happens in that case with your requirements?
g

Gilles Barbier

09/20/2021, 7:14 AM
@Bhullnatik it's counter intuitive, but instanceParameter is null when you provide an instance (obf::foo) - not null if you use the syntax Foo::foo 🤷‍♂️
@Kirill Grouchnikov I understand it can be null, but I'd like to be able to retrieve it when not null
s

smit01

09/20/2021, 9:07 AM
Not sure if this is possible but if someone knows how please explain us in briefly. I tried but still can't figure it out.