Closure parameter oddity

Why doesn't compiler allow the first "execute" line here?

protocol Proto {}
struct A: Proto {}

func foo<T: Proto>(execute: (T) -> Void) {
    // execute(A()) // Cannot convert value of type 'A' to expected argument type 'T'
    execute(A() as! T) // ok
}
1 Like

Try to work this through:

protocol Proto {}
struct A: Proto {}

func foo<T: Proto>(execute: (T) -> Void) {
    execute(A()) // Should this work when `execute == bar`?
}

struct B: Proto {
  var a: Int
}
func bar(b: B) { print(b.a) }
foo(bar)
8 Likes
Terms of Service

Privacy Policy

Cookie Policy