https://kotlinlang.org logo
#reflect
Title
# reflect
d

diesieben07

03/23/2018, 10:01 AM
KType.classifier
will give you that, but it does not have to be a
KClass
, it could be a
KTypeParameter
. There is also
KType.jvmErasure
, which will loose type information though.
v

v79

03/23/2018, 10:10 AM
I still can't do
when(consP.type.classifier) { is kotlin.String -> \\... }
though, as I still have incompatible types.
d

diesieben07

03/23/2018, 10:11 AM
Yes,
classifier
is a
KClassifier?
. The type might not be a pure
KClass
.
v

v79

03/23/2018, 10:14 AM
Hmm, perhaps what I want is not possible then. I really just want to know if my constructor's parameter accepts a kotlin.String or not. If it does, I'll just pass in my string value. If it does not, I'll call another function.
d

diesieben07

03/23/2018, 10:16 AM
Well, the problem is not as simple as you might think. Assume you have this class:
Copy code
class MyClass<T>(value: T)
You can pass in a
String
only if
T
is
String
, but that is not knowable at runtime, since generics are erased.
v

v79

03/23/2018, 10:22 AM
OK. I'll have a think about my approach. I think Spring MVC manages it, somehow, but it's been a while since I've written much Spring code.
d

diesieben07

03/23/2018, 10:23 AM
Spring will "blow up" in the scenario I mentioned as well. If you want Spring's behavior,
jvmErasure
is what you want I think.
v

v79

03/23/2018, 10:28 AM
Cheers. More to read!
2 Views