Swift
April 7, 2019
Object oriented programming is great, but sometimes things don’t fit neatly into
a superclass/subclass hierarchy. You may have a piece of code that would be
needed in several contexts, but for technical reasons beyond your control you
cannot merge them into a single hierarchy.
Some languages have the concept of multiple inheritence, where a subclass can
specifically inherit from several parents. But this has it’s
own set of problems.
Many other languages, however, solve this through the use of traits or mixins.
These allow us to have a set of methods that are basically copied into the
object at compile time. This way they can be used anywhere they are needed.
Swift doesn’t have the concept of mixins or traits per se. But, starting with
Swift 3, you can get very equivalent functionality using protocol default
implementations.
Read More