December 2018
Intermediate to advanced
414 pages
10h 19m
English
Protocol with Associated Types or PATs is a powerful but largely misunderstood feature of Swift; it feels buggy but works as intended. The first encounter with PAT often terminates with the following dreaded error:
// Protocol 'MyProtocol' can only be used as a generic constraint because it has Self or associated type requirements
This error is by design and you're not doing anything wrong; it's just a small misunderstanding of how to use associated types or self requirement with protocols, which we'll hopefully explain and fix for now and the future.
Let's consider this popular example:
protocol Food {}protocol Animal { func eat(food: Food)}struct Cow: Animal { func eat(food: Food) {}}
We defined two protocols, Food and ...
Read now
Unlock full access