Nikita Ermolenko
Nikita Ermolenko

Reputation: 2259

Associatedtype Swift 3

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 {

}

Upvotes: 2

Views: 466

Answers (1)

kevin
kevin

Reputation: 2071

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 {
}

Upvotes: 2

Related Questions