geepawhill
07/25/2019, 5:49 PMclass X : List<Y> by myList { private val myList = mutableListOf<Y>()
Failing that as a simple declaration, is the clever way to do it maybe make the primary constructor private and offer secondary constructors to call it?Hanno
07/25/2019, 6:18 PMkarelpeeters
07/25/2019, 6:20 PMStephan Schroeder
07/26/2019, 9:53 AMfun main() {
val x = X()
x.add("done")
}
class X private constructor(list: MutableList<String>) : MutableList<String> by list {
companion object {
operator fun invoke(): X = X(mutableListOf())
}
}
There might be a way to get a generic version, probably by providing a KClass to invoke. Somehow the code has to now which kind of list is meant here (your pseudo code never defines the the type either).
Here the link to the playground: https://pl.kotl.in/vEGbYTUEDgeepawhill
08/16/2019, 9:05 PM