I'm wondering what the visibility rules are for traits:
import java.util.ArrayList
trait T {
protected val list: MutableList<String>
public fun doStuff(): Unit {
list.add(“”)
}
}
class TUser : T {
override val list: MutableList<String> = ArrayList()
}
fun main(args: Array<String>) {
val subject = TUser()
subject.doStuff()
}
IDE and compiler do not complain about this but it fails at runtime:
Exception in thread “main” java.lang.IllegalAccessError: tfo.kotlin.traits.TUser.getList()Ljava/util/List;
at tfo.kotlin.traits.T$$TImpl.doStuff(TraitExample.kt:9)
at tfo.kotlin.traits.TUser.doStuff(TraitExample.kt)
at tfo.kotlin.traits.TraitsPackage-TraitExample-a18e5d74.main(TraitExample.kt:19)
at tfo.kotlin.traits.TraitsPackage.main(TraitExample.kt:1)
The runtime error disappears when I change the visibility of the property in class TUser to public. Is this intended behavior or is this a bug? I would argue that TUser inherits from the trait und thus should have access to its protected state/functionality.