Kotlin: Constructor of inner class can be called only with receiver of containing class
We can use it this way too
OuterClassName().NestedClassName()
Your code basically means that InnerClassX
is an inner class of InnerClassSuper
, not OuterClass
, so the error means you need to provide a receiver object of InnerClasssSuper
upon construction of InnerClassX
.
At this point, Kotlin allows having neither an inner sealed
class nor a derived class for a sealed
class as an inner
class of another class.
You can, however, make an abstract
class derived from the sealed
one and inherit from it inside the OuterClass
:
sealed class SealedClassSuper {
abstract class SealedClassChild(val x: String): SealedClassSuper()
}
class OuterClass {
inner class InnerClassX(x: String) : SealedClassSuper.SealedClassChild(x)
fun someFun(): SealedClassSuper {
return InnerClassX("Hello")
}
}