zt
09/20/2024, 11:11 PMa is a var I get this error:
Type of 'a' doesn't match the type of the overridden 'var' property 'var a: Letter' defined in '/Base'
It doesn't occur when it's a val
open class Base {
open var a: Letter = Letter()
}
class BaseExt : Base() {
override var a: A = A()
}
open class Letter
class A : Letter()Joffrey
09/20/2024, 11:14 PMA is a subtype of Letter, so if it's a val all is good. The problem when it's a var is that someone could assign any subtype of Letter to a, which would break instances of `BaseExt`:
val ext: Base = BaseExt()
ext.a = Letter() // respects the interface of Base, yet shouldn't be allowed for BaseExt