is there a way to convert a KFunction to a lambda ...
# getting-started
b
is there a way to convert a KFunction to a lambda other than just casting it?
r
This sounds like it may be a XY problem...
b
i've got a sort of factory function and want to return a constructor of a class--wanted to just use a reference to the constructor but felt the KFunction was overkill and just wanted to return a lambda
i decided to change it to have the factory do the instantiation and return the instance instead, but was still curious
k
This just works:
Copy code
class Foo
fun fooFactory(): () -> Foo = ::Foo
so I'm not sure i understand the question?
b
ah, yeah i had some generic/inheritance there that probably didn't make sense. it was like this:```interface Parent<T> class Child : Parent<Boolean> fun<T : Any> blah(): () -> Parent<T> = ::Child```
s
In that example T must be Boolean and cant be anything else.
b
yeah, realized after writing the simple example it was the type erasure issue. so guess doing
fun<T : Any> blah(): () -> Parent<T> = ::Child as () -> Parent<T>
fixes it
m
No, that will give a runtime time error if
T
is anything else than
Boolean
. Why do you even need
T
, when all you do is call the child constructor?
Copy code
fun blah(): () -> Parent<Boolean> = ::Child
That is the only thing that works if you’re going to call the child constructor.
b
yeah, that was just for the purpose of a simplified example...in my code i had a longer block there that compared a reified type and then instantiated the right version and returned it, so i knew the implementation would always match T