I posted this to <#C0AVAB92L|reflect> but that cha...
# getting-started
a
I posted this to #reflect but that channel doesn’t see any action. Perhaps someone can point me to resources to understand this probably very simple problem regarding reflection and copying data in a generic way from an instance of one class to another type. I am lost on receivers. grr. https://kotlinlang.slack.com/archives/C0AVAB92L/p1670535073508719
s
1.
this@toPublic
would be used to disambiguate if there were multiple possible receivers. It means “the receiver of the
toPublic
method”. In your case it’s not necessary because there’s only one receiver in scope. You can just write
this
. 2. Your second example doesn’t compile because the compiler doesn’t know what types would be valid for the receiver parameter. You can fix it by making an additional reified type parameter for the
input
type, e.g.
I
, and then using
I::class
in place of
input::class
.
There are a couple of things going on with the second point. The extra type parameter is needed in order to capture the type of the input value, so that the type of the member properties can be expressed in terms of their relationship to the input value, rather than just as
Any
. And then it has to be reified because
input::class
has type
KClass<out I>
, whereas
I::class
has type
KClass<T>
(which is what you need if you want to call member functions on it)
a
@Sam Thanks for the response. I’ll have to digest your response for a bit. I still haven’t gotten it to work, so I’ll spend time on it. Again, thanks so much. I know it’s probably a really simple problem, but I just lack the internal monologue to grok generics, type erasure, reified types, and all that noise. This has been going on since Java 1.5 was released. 🙂
KClass doesn’t have member properties so that is where I am stuck. Hah.
s
Ah, you may need to make the type be
reified I: Any
in where it’s declared, rather than using
I & Any
at the use site
a
Thanks again. 🙂 This API isn’t turning out what I want to be, but you got me past that step where I can get it to work, but it’s not pretty. Maybe it’s because It’s the end of the week and I’m burnt out.
Just trying to swizzle the signature so it could easily be used like:
Copy code
fun Private.toPublic() = assign<Public>(this)
I’m sure other’s have written such libraries, but, I’m just trying to get a grip around it.
You’ve been a lot of help, I appreciate it. Consider this case “closed”.