Here is some setup code to explain what is happening:
protocol CanJump{
func jump()
}
struct Dog: CanJump{
func jump(){
print("yay jump")
}
}
struct Cat: CanJump{
func jump(){
print("nothx")
}
}
let d = [Dog()]
let c = Cat()
This does not work:
let thingsThatCanJump: [CanJump] = d
Cannot convert value of type [Dog] to specified type [CanJump]
This does work:
let thingsThatCanJump: [CanJump] = [c]
A disgusting workaround that I've come up with is:
let thingsThatCanJump: [CanJump] = d.map({$0 as CanJump})
Can someone explain why this is happening?
I'm guessing this has something to do with the compiler not completely evaluating types and conformance.