concept Self in category swift

appears as: Self
Swift in Depth

This is an excerpt from Manning's book Swift in Depth.

The reason that makeGame returns Self is that Self is different for each subclass. If the method were to return a BoardGame instance, makeGame wouldn’t be able to return an instance of MutabilityLand for example. But you’re not there yet; this gives the following error.

Then the chapter takes a look at the shortcomings that come with protocols with associated types and Self requirements, which you can unofficially consider compile-time protocols. You’ll see how to best deal with these shortcomings. You’ll first consider an approach involving enums, and then a more complex, but also more flexible, approach involving type erasure.

  • A protocol with associated types or Self requirements can’t be used as a concrete type.