Why does the swift compiler require a typecast operator to convert to a generic type

Viewed 30

Why does the swift compiler throw an error in the case of function foo1? When a typecast operator is used in function foo2 no error is thrown.

protocol Proto {}
struct A: Proto {}

func foo1<T: Proto>(execute: (T) -> Void) {
    execute(A()) // Cannot convert value of type 'A' to expected argument type 'T'
}

func foo2<T: Proto>(execute: (T) -> Void) {
    execute(A() as! T) // ok
}
0 Answers
Related