When targeting Javascript and native java.lang.Class.forName() is not available. How can I get a KClass in these environments?
-
I've deleted my answer because for some reason I've missed the JavaScript part of the question. Sorry for that.Alexander Udalov– Alexander Udalov2017-06-20 14:37:52 +00:00Commented Jun 20, 2017 at 14:37
Add a comment
|
2 Answers
As it said in Kotlin documentation:
At this time, JavaScript does not support the full Kotlin reflection API. The only supported part of the API is the
::classsyntax which allows you to refer to the class of an instance, or the class corresponding to the given type. The value of a::classexpression is a stripped-downKClassimplementation that only supports thesimpleNameandisInstancemembers.
Here's the example:
val a = A()
print(a::class.simpleName) // Obtains class for an instance; prints "A"
print(A::class.simpleName) // Obtains class for a type; prints "B"
Comments
I didn't test it on Native nor JavaScript version (it does work on the JVM one) but you can just do SomeClass::class. Kotlin docs