Looks nice. Currently I’m trying to overpass it with
protected val foo: MutableList<Int> = mutableListOf()
fun getFoo(): List<Int> = foo
it works with a private but not with a protectedfoo because of same JVM signature error. Looks ok with internal visibility, there’s something wrong with Kotlin’s protected implementation.
There’s nothing wrong with it. Compiler can’t use automatic name mangling for items that could be accessed from outside of the module. You have to rename manually with @JvmName.
what if we need something like this? Is it included in proposed getter?
class A(){
protected var foo: MutableList<Int>? = null
val getFoo(): List<Int>? = foo
}
class B: A() {
init {
foo = mutableListOf(if (true) 1 else 3)
// There we don't need to check if list was created or make it empty
}
}
And is it even will be possible to add such getter on a constructor parameter?
Looking a little larger use case than a single variable, if this is part of a class where that variable is exposed via an interface you can simply do this:
interface A {
val foo: List<Int>
}
class AImpl : A {
override val foo = mutableListOf()
}