-
Book Overview & Buying
-
Table Of Contents
Mastering Swift 6 - Seventh Edition
By :
Prior to Swift 5.7, when SE-0352 was added, it was not possible to use protocols that contained associated types or self requirements as types. This made working with protocols that contained associated types cumbersome at times. Developers often had to resort to using type erasure techniques or additional boilerplate code to make their generic protocols usable as types. This was because Swift’s type system didn’t allow a protocol with associated types to be used as a type without specifying what those associated types were. Let’s look at an example of this. To begin with, let’s create a Drawable protocol with two types that conform to it:
protocol Drawable {
func draw()
}
struct Circle: Drawable {
func draw() {
print("Drawing a circle")
}
}
struct Square: Drawable {
func draw() {
print("Drawing a square")
}
}
In this code, we define the Drawable protocol and...