I have a very simple playground:
protocol MyProtocol {}
struct MyType: MyProtocol {}
class MyClass <T: MyProtocol> {
func myFunction(array: [T]) {
if let myArray = array as? [MyType] {
println("Double!")
}
}
}
let instance = MyClass<MyType>()
let array = [MyType(), MyType()]
instance.myFunction(array)
Then it says "MyType is not a subtype of 'T'" on the if let line. Well, I think, however, MyType and T are compatible.
When I modified the if let statement, it actually works:
if let first = array.first as? MyType
But now I can't cast array to [MyType] (Sure, I know it is Swift's static typing specification.)
I'm wondering what the problem is. My understanding about Generics? Or is it Swift language's limitation? If so, is there any way to do like this?
Thanks in advance.