I will try to demonstrate what I mean with an example.
Say I have this protocol:
protocol P {}
And these types that are conforming to it:
struct S1 : P {}
struct S2 : P {}
And finally, this generic method that accepts one parameter which is any type conforming to protocol P:
func f<T>(type: T.Type) where T : P {
// ...
}
Now there is no problem in passing S1.self
or S2.self
to f
.
f(type: S1.self) // No problem!
f(type: S2.self) // No problem!
But if I want to add S1.self
and S2.self
to an array
let types: [P.Type] = [S1.self, S2.self]
I won't be able to pass to f
a type from this array.
f(type: types[0]) // ERROR: Cannot invoke 'f' with an argument list of type '(type: P.Type)'
I don't understand what I'm doing wrong. Any help would be greatly appreciated.