Type Variance in Swift
nomothetis.svbtle.com
nomothetis.svbtle.com
func countFurballs(shelter:Shelter<Animal>) {
println("Furballs: \(shelter.inhabitants.count)")
}
var catShelter = Shelter(inhabitants:[Cat()])
countFurballs(catShelter) // throws compilation error
I believe that the countFurballs function could be made more generic: func countFurballs(shelter:Shelter<T>) {
println("Furballs: \(shelter.inhabitants.count)")
}
var catShelter = Shelter(inhabitants:[Cat()])
countFurballs(catShelter) // works?
I don't have Xcode in front of me, so that may still be a compiler error, but I believe it works.(Sorry for the delay; I assumed nobody had read it… :-) )