Say I have the following code:
protocol A {}
struct B: A {}
func b() -> A {
return B()
}
func f<T where T: A>(t: T) -> T {
return t
}
f(b())
This results in the following error (Xcode 6.3 beta 3):
Playground execution failed: Test.playground:12:1: error: generic parameter 'T' cannot be bound to non-@objc protocol type 'A'
f(b())
^
Test.playground:8:6: note: in call to function 'f'
func f<T where T: A>(t: T) -> T {
^
I feel like this is a shortcoming with the implementation of generics in Swift. Is there a way to nicely work around this issue while still keeping the generics?
In my code f()
has additional type requirements; I could just forget about protocols and generics altogether, but I don't accept defeat that easy ;-).