Property overrides in subclass allowed in Silver, but not in Swift


(John Perez) #1

Overriding properties in subclasses does not seem to be allowed in Swift, so the following code

class Plant {
    var desc1="I am Plant"
}

class Tree:Plant {
    override var desc1="I am Tree"
}

var p=Plant(), t=Tree()

print(p.desc1)
print(t.desc1)

returns the following error:

main.swift:6:18: error: cannot override with a stored property ‘desc1’
override var desc1=“I am Tree”
^
main.swift:2:9: note: attempt to override property here
var desc1=“I am Plant”

But in Silver, it compiles fine. Probably safe to ignore for now?


(marc hoffman) #2

As designed/a feature, I’d say for this one, yes ;). Really no good reason to not allow it (and it seems insane, personally use virtual properties all the time…

BTW: thank your for all the detailed reports, this is much appreciated. It can be difficult to get all the detailed intricacies just right, so these rally help — please keep them coming, and we’ll try to fix as many and as quickly as possible. Thanx!


(Aaron Bratcher) #3

Calculated properties can be overloaded in Swift tho. Must be how they’re stored. Once Swift reaches ABI stability, I suspect Silver will need to match exactly.

class Plant {
	var desc1: String {
		return "I am Plant"
	}
}

class Tree:Plant {
	override var desc1: String {
		return "I am Tree"
	}
}

var p=Plant(), t=Tree()

print(p.desc1)
print(t.desc1)