This is particularly what I mean:
class Base {}
class Derived: Base {}
protocol Proto {
var items: Array<Base> { get }
}
extension Proto {
var items: Array<Base> {
return []
}
func show() {
print("Number of items: \(items.count)")
}
}
class Example: Proto {
let items = [Derived()]// as Array<Base>
}
let ex = Example()
ex.show()
This stumped me for a bit in one of my programs. Here, Example.items
is actually Array<Derived>
, so it's not overriding the Proto
's default value. If I cast it to Array<Base>
, then it all works.
Should this be allowed? I can't imagine how this would ever be useful...