Generics for Protocols
Protocols can create/define generic types called associated types. You may sometimes want to create a protocol that uses some type that will be decided when you create the object. Earlier in the chapter, you created a List
that has a couple of nifty methods and properties. Now you will create a protocol to describe some of the functionality. So here you’ll create a Bucket
protocol to describe the adding functionality to the List
, so that you can add as many elements as you want—and remove them. You’ll also make a Uniquable
protocol, which describes the deDup
functionality. Here are the two protocols and their implementation declaration in the List
:
protocol Bucket { typealias SomeItem var ...
Get Learning Swift™ Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.