I'm trying to call get() on a property via reflect...
# announcements
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
? 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.
is the "bottom type" in Kotlin, which can be thought of as the logical opposite to
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
, you don't know if it a list of `String`s, `Comparable<String>`s, `Comparable<*>`s,
, etc. (and that can more than double if you take all the nullable variants into account as well).
yeah. I got past my
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?
(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
thanks for this. what's a receiver instance?
You said:
This error makes me think the instance isn't associated w/ the 
 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
oh... so used the instance passed in like :
{param -> func.call(app,param)}
yep exactly that
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] !
No problem whatsoever. Happy to help!
a semi-related question. There is no performance penalty by adding these via reflection vs
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.
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)