Mixins and traits
medium 50 minA mixin or trait is a small bundle of methods that you add to classes that have nothing else in common.
Watch lesson4:44
Where it shows up
The same pattern appears across everyday software.
Ruby Comparable
The class defines `<=>`, the mixin brings the usual comparison operators, `between?` and `clamp`.
Rust Iterator
One required method, `next`. The current documentation lists about 75 provided ones, such as `map`, `filter` and `collect`.
Python collections.abc
The documentation has a column of mixin methods: write two or three abstract methods, get the rest.
Python socketserver
`class ThreadingUDPServer(ThreadingMixIn, UDPServer)`: the mix-in comes first because it overrides a method of the server.
Django
`LoginRequiredMixin` adds a login check to a view class and should be leftmost in the list of parents.
Java Iterable
`forEach` and `spliterator` are default methods. Classes that only wrote `iterator()` got them when Java 8 arrived.
Laravel
A model uses the trait `SoftDeletes`, and deleting then only marks the row.
Dart
Has a keyword: `mixin`. With `on` a mixin states which superclass it needs.
Scala
Its tour calls traits mixins when they are used to compose a class: one superclass, many mixins.
Vue
Mixins still work in Vue 3, and the API page marks them as no longer recommended. It points to composable functions.