I'm trying to call get() on a property via reflect...
# announcements
t
I'm trying to call get() on a property via reflection. I pass an instance of the class in question to get() and I get the error
Type mismatch: inferred type is MessageDispatch but Nothing was expected
what's a
nothing
? And the docs say it should be an instance of the class... which it is.. very confused.
what I actually want to do is add a element to a list based on reflection of the type of the list (and the type of the element given). but the docs are pretty obtuse and I can't find any examples that point me in the right direction.
r
https://kotlinlang.org/docs/exceptions.html#the-nothing-type
Nothing
is the "bottom type" in Kotlin, which can be thought of as the logical opposite to
Any?
.
You may be in for an uphill battle. Generic types are erased at runtime, so you can't easily determine what the list's "type" is. And sampling an element (or even all the elements) won't entirely narrow that down. For example, if you get a
String
, you don't know if it a list of `String`s, `Comparable<String>`s, `Comparable<*>`s,
Any
, etc. (and that can more than double if you take all the nullable variants into account as well).
t
yeah. I got past my
Nothing
problem, and now running into issues w/ KFunction and the instance (I think.. not sure). I don't think I'm having type erasure issues at the moment, but I can see how it'd come up. I made a stripped down version of my problem here : https://stackoverflow.com/questions/66750194/kotlin-reflection-how-to-add-a-kfunction-to-a-list-via-reflection
I don't seem to have any problems getting the type of a list... I wonder why?
y
(Copied from my Stack Overflow answer) Try casting the list to a lambda list of
(Any) -> Any
at first and then add in a lambda that then calls your KFunction with the associated receiver like this:
Copy code
println("found match, setting type = $parmType")

val lst = prop.call(dis) as MutableList<(Any) -> Any>

lst.add( { param -> func.call(myReceiverInstance, param) } ) // You just need to supply the myReceiverInstance
t
thanks for this. what's a receiver instance?
y
You said:
This error makes me think the instance isn't associated w/ the 
KFunction
 I'm adding to the list... but I don't know how to associate it. I have the instance handy, I just don't know how to attach it. Or maybe it's something else entirely.
and that's what I'm referring to. The error is indeed that the function is missing the instance, and this is how you attach it
t
oh... so used the instance passed in like :
{param -> func.call(app,param)}
?
y
yep exactly that
t
the compiler says the inffered type is Any? but Any was expected. where is it getting a Any? from?
nvm.. used prens instead of a capture
hey! it works! thanks @Youssef Shoaib [MOD] !
y
No problem whatsoever. Happy to help!
t
a semi-related question. There is no performance penalty by adding these via reflection vs
lst.add(this::myFunc)
is there? I assume the normal way is just syntactic sugar over the same thing?
sorry... should have been more clear. Any performance difference when calling these functions from iterating over the list? I know this way of adding them to the list is likely way way slower.
y
So basically you're asking if adding the functions manually vs adding them through reflection and then calling them will have a performance penalty right? Well basically adding them thru reflection obv is slower but when calling them it should most likely be the same exact performance (I think)