2

When targeting Javascript and native java.lang.Class.forName() is not available. How can I get a KClass in these environments?

1
  • I've deleted my answer because for some reason I've missed the JavaScript part of the question. Sorry for that. Commented Jun 20, 2017 at 14:37

2 Answers 2

1

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 ::class syntax which allows you to refer to the class of an instance, or the class corresponding to the given type. The value of a ::class expression is a stripped-down KClass implementation that only supports the simpleName and isInstance members.

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"
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.