Is it possible to write a helper method in kotlin ...
# announcements
b
Is it possible to write a helper method in kotlin so that java code can create a
KType
?
d
Depends on what your input is. Is it a
java.lang.reflect.Type
? A
java.lang.Class
?
b
Ideally
java.lang.Class
I think
d
You can write a method in Kotlin that accepts a
Class
. Turn it into a
KClass
using
cls.kotlin
and then into a
KType
using
createType
or
starProjectedType
depending on your needs
b
I saw
createType
, but was worried about whether or not I'd be able to infer the correct arguments (i.e. be able to properly differentiate
List<Int>
from
List<Double>
)
d
There is nothing to infer,
java.lang.Class
does not represent this information. The
Class
for
List<Int>
and
List<Double>
is the same, both are just
List
b
KClass does I think, right?
d
No,
KClass
does not either. Only
KType
does, that's why it exists
b
I had played previously with a kotlin helper which java could call to get a KClass, and was able to differentiate things like
List<Int>
and
List<Double>
I thought (I'll have to double check)
d
Not using
KClass
or
Class
, you need
KType
or
java.lang.reflect.Type
for that.
b
Hm, ok
Ah, I see...it was an old experiment I was trying but you're right, I don't think it worked.
Bummer...Ok, thanks @diesieben07 I'll look into doing it with
java.lang.reflect.Type