Swift Default Protocol Implementations
All of this highlights a significant tension between methods and functions in Swift.
The Object-Oriented paradigm is based around the concept of objects that encapsulate both state and behavior. However, in Swift, it’s simply impossible to implement certain generic functions as methods on the
struct
orclass
itself.[…]
Because of the constraint on the element of the sequence generator being
Equatable
, this cannot be declared on a generic container, without thereby requiring elements in that collection to conform toEquatable
.Relegating behavior like
contains
,advance
, orpartition
to top-level functions does a disservice to the standard library. Not only does it hide functionality from method autocompletion, but it fragments the API across a Object-Oriented and Functional paradigms.