Given a sealed class Foo and an instance of `val s...
# announcements
p
Given a sealed class Foo and an instance of
val someClass: KClass<out Foo>
is it possible for
someClass.objectInstance
to be null? I think the answer is yes but I am struggling to come up with a way to make a test for such a thing. The documentation for objectInstance says, ” …if this class is not an object declaration” so my question really boils down to: how do I define a class that is not an object declaration? And the corollary, can I define a member of a sealed class that is not an object declaration. If you’re inclined to reply, please do so in a thread. Thanks. fwiw, my brain hurts just trying to grok this.
p
I have a guess that singleton declaration:
Copy code
object Bar {
    // ...
}
is object declaration. And usual class declaration:
Copy code
class Bar {
    // ...
}
is not.
p
@Pavlo Liapota your analysis makes sense to me but seems to be opposite to the documentation: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.reflect/-k-class/object-instance.html
I will run this through the debugger to see what it says …
And the debugger says the documentation is correct. Boy am I confused.
So I clearly misunderstood what objectInstance provides. I thought it gave an instance for the receiver (a KClass) but not the case. Next question is, how does one get an instance from a KClass? I will be researching that but feel free to shortcut my effort. 🙂
p
Do you want to create a new instance by
KClass
using default constructor?
p
I know how to do this with the JVM but not with Native or JS. Being able to create a
KClass
instance multiplatform would be nice. If you know how to do that I am all eyes. 🙂