Conditional Conformance in the Standard Library
The Swift 4.1 compiler brings the next phase of improvements from the roadmap for generics: conditional conformances. This post will look at how this much-anticipated feature has been adopted in Swift’s standard library, and how it affects you and your code.
→ swift.org/blog/conditional-conformance/
Swift 4.1 adds conditional conformances, which let generic types like Array
and Optional
conform to protocols such as Equatable
or Hashable
only when their elements do. This fixes old limits, so you can now compare nested structures like arrays of optionals, and it gives collections helper methods like contains or index(of:)
for equatable elements.
It also cuts code duplication by letting types build capabilities step by step, as shown in a lazy splitting example where a wrapper gains bidirectional support if the base collection does. This change shrinks the standard library size and opens doors for custom lazy wrappers, like chunking collections.
Category:
Tag:
Year: