From: https://medium.com/dartlang/announcing-dart-2-1-improved-performance-usability-9f55fca6f31a
Under Mixins:
mixin SomeClass<T extends SomeOtherClass>
on State<T>
implements ThirdClass
What is "on"?
We make use of the with keyword followed by one or more mixins names. Mixins can be used in two ways, the first case is when we want to make use of class code in such a way that the class doesn't have any constructor and the object of the class is extended. In such a case, we use the with keyword.
Mixins is not a way to get multiple inheritance in the classical sense. Mixins is a way to abstract and reuse a family of operations and state. It is similar to the reuse you get from extending a class, but it is compatible with single-inheritance because it is linear.
To implement a mixin , create a class that extends Object and declares no constructors. Unless you want your mixin to be usable as a regular class, use the mixin keyword instead of class . In this way, your Car can run , but cannot be ' handleControlled '.
Mixins are a way of reusing a class's code in different class hierarchies. For example, you might have a class called Employee which has methods like clockIn . The code in those classes may be useful for both Bartender and Nurse .
This mixin can only be applied to classes that extend or implement State<T>
which is effectively the state of a stateful widget.
Figuratively speaking on is the extends for mixins.
mixin A on B
is like
class A extends B
To choose which one to use is like to choose between composition or inheritance. More on that composition vs inheritance thing.
class ExtraPowers{}
class ClassPowers extends ExtraPowers{}
mixin MixinPowers on ExtraPowers{}
Let's say our main character is class "C" and we want to give it some extra capabilities and powers. We can do that in two ways:
// e.g. inheritance
class C extends ClassPowers{}
or
// e.g. composition
class C with ExtraPowers, MixinPowers{}
So if we choose to extend we may need to comply to some requirements. And we can do that by passing arguments to super.
If we choose to gain powers by using with can't use super to pass requirements. The requirements are satisfied by having the same extra powers as our mixin (nothing is left hidden e.g composition) and that on keyword tells us what extra powers our mixin has. So in order to get powers from MixinPowers we first must get ExtraPowers.
class C with ExtraPowers, MixinPowers{}
In conclusion on just like extends gives access to members of another object but on is for mixins extends for classes. The difference is when you use mixin you must implement the objects after the on keyword.
More info
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With