Mon 25 Oct 2021

Specialized extensions using generic type constraints

Combining Swift’s powerful generics system with the fact that any Swift type can be extended with new APIs and capabilities enables us to write targeted extensions that conditionally add new features to a type or protocol when it fits certain requirements.

It all starts with the where keyword, which lets us apply generic type constraints in a range of different situations. In this article, let’s take a look at how that keyword can be applied to extensions, and what sort of patterns that can be unlocked by doing so.

Source: Specialized extensions using generic type constraints, an article by John Sundell.