(trying to adapt <https://stackoverflow.com/questi...
# announcements
p
m
Do you mean this one? https://gist.github.com/udalov/bb6f398c2e643ee69586356fdd67e9b1 It somehow works for me. The
ERROR
type I only get if I don’t create an anonymous subclass at the call site (i.e. in the
inline fun getKType
). I noticed however that the resulting
KType
(and
KTypeProjection
) has various issues: • You cannot get the
KClass
or
Class
which was used to create the
KType
so you have to store that info separately. • The
KType
lacks nullability information. • The
KTypeProjection
doesn’t have star projection anymore (turns into
out UpperBound?
at runtime). •
KType
itself isn’t generic and thus you lose compile-time safety when passing around `KType`s instead of `KClass`es. • I think `KType`’s implementation isn’t optimized that much for performance.
isSubtypeOf
for example looks scary. •
KTypeProjection
lacks
isSubtypeOf
or something similar to check whether two projections are compatible/assignable.
In the end it all depends on your use case. I was lucky because I only need a subset of `KType`’s information and wrote my own very specific wrapper: https://github.com/fluidsonic/fluid-json/blob/6594ab0bfe66252a00832b612fc76bc1da24401e/Sources/JSONCodableType.kt It basically ignores variance and nullability since they’re not reliable anyway. It pre-computes the hashCode for performance and caches
ParametrizedTypes
for the same reason. It doesn’t support some
Type
classes and multiple upper/lower generic bounds since they’re all not relevant in my case.
p
The
toString()
of the resulting
KType
always contains “ERROR: …” for me. I was particularly interested in the nullability information, but as you said, it’s not there. kotlin-reflect feels a bit shaky and doesn’t seem quite there yet. also got lots of errors when debugging it.
m
Unfortunately there cannot be nullability information when
KType
is constructed from Java’s
Type
since Java doesn’t have any info about nullability.
p
I didn’t construct
KType
from java’s
Type
, I constructed it using kotlin reflection. same thing.
m
How did you construct it?
KClass.createType
allows you to specify nullability 😮
p
lol
I had tried
KClass.supertypes.single().arguments.single().type!!
m
Ah, yeah. Regarding generic inheritance I think Kotlin has the same limitations as Java - i.e. type erasure 😕
p
this is using reified super type token, and works for
Class
. why can’t it work for
KClass
too?
KClass.supertypes
seemed like the equivalent of
Class.getGenericSuperclass()
m
Yeah that would be great but I think it isn’t 😞
KType
is mostly useless at the moment imo.
p
I simply think it’s not mature, some things haven’t been implemented yet.