2

I'm trying understand generics culture in Swift so I've written a small example. But it does not compile.

Error: Generic parameter 'P' could not be inferred I can't understand for I'm doing wrong.

protocol Protocol_1 {
    associatedtype T
}

protocol Protocol_A {}
struct SomeStruct_2: Protocol_A {}

struct SomeStruct_1: Protocol_1 {
    typealias T = Protocol_A
}


let struct1 = SomeStruct_1()
testFunction(t: struct1) // *Generic parameter 'P' could not be inferred*

func testFunction<P: Protocol_1>(t: P) where P.T : Protocol_A {

}
Nikita Ermolenko
  • 2,139
  • 2
  • 19
  • 41
  • 1
    [Protocols do not conform to themselves](http://stackoverflow.com/questions/33112559/protocol-doesnt-conform-to-itself) in Swift, so you cannot use `Protocol_A` as a type that conforms to `Protocol_A`. – Hamish Nov 24 '16 at 10:01

1 Answers1

2

P.T in testFunction cannot conform to Protocol_A, but you can check if its equal to Protocol_A.

func testFunction<P: Protocol_1>(t: P) where P.T == Protocol_A {
}
kevin
  • 2,021
  • 21
  • 20