Alexis Gallagher - Protocols with Associated Types

Functional Swift · 32K views · 2015 · 56:07

Open on YouTube

Programming language designFunctional programmingtalkdated

Why Stack Overflow recommends it

Stack Overflow users link it when answering questions about Swift's 'can only be used as a generic constraint' errors, type erasure, and why PATs cannot be used as first-class types.

Recommended by 2 people across 2 different months, 2015 to 2017.

What it is

A conference talk by Alexis Gallagher from the 2015 Functional Swift conference on protocols with associated types (PATs) in Swift. It explains why PATs behave differently from ordinary protocols and how the language ended up with that design.

What people said

Strong rec finneycanhelp · 2015-12 · Stack Overflow · on “What does "Protocol ... can only be used as a generic constraint because it has Self or associated type requirements" mean?”

Look at 0:56 into the great presentation by Alexis Gallagher titled: Protocols with Associated Types and how they got that way (maybe) [this video]

Recommends finneycanhelp · 2015-12 · Stack Overflow · on “How to Override Observable<T> for Testing's Sake?”

While exploring solutions in this, I was running into PATs (Protocols with Associated Types) that were not compiling - This talk by Alexis Gallagher - Protocols with Associated Types - helps me understand why: [this video]

Recommends Charles Srstka · 2017-10 · Stack Overflow · on “How to do Type erasure in Swift using if-let?”

Here is a video that explains things pretty well. It's a bit old, so the syntax is not Swift 4, but you'll still get the gist: [this video]

Cites Marcus Rossel · 2016-01 · Stack Overflow · on “How to work around Swift not supporting first class meta types?”

The solution seems to be a type-erased wrapper [LINK] . Type-erasure fixes the problem of not being able to use protocols with associated types (PATs) [this video] as first-class citizens, by creating a wrapper type, which only exposes the properties defined by the protocol, which it wraps. In this case, LanguageType is a PAT , due to its adoption of Equatable (which it conforms to, due to its adoption of Hashable) : protocol LanguageType: Hashable { /*...*/ } Therefore it can not be used as a first-class type in the Translatable protocol: protocol Translatable { var translations: [LanguageType: [String]] { get set } // error } Defining an associated type for Translatable would not fix the proble

Cites SimplGy · 2016-04 · Stack Overflow · on “UIViewController class with <generic> gives error "Unknown class in Interface Builder file"”

I'd like to have a view controller that can display an object that conforms to a protocol with an associated type ( PAT [this video] ). I can't really avoid using a PAT, but I'd love to have a view controller that's able to display any object that conforms to this protocol. Everything compiles fine, and interface builder auto-completes the classname just fine, but at runtime I get this error: Unknown class _TtC14api_collection17ModelPrinter in Interface Builder file. I've looked at other Q&A for this error message, and in all the cases I've found it was about the storyboard coming out of sync with what's really going on, and that isn't the case here. Here is my class definition: class ModelPrinte