How do I use Mixins without inheritance? - dart

Is it possible to apply a mixin to a class without the target class inheriting from any other class? For example, can I implement the following:
class User with Persistence {
// implementation
}
Most of your examples of Mixins in dart seem to be coupled with inheritance.
Thanks in advance!

You have to inherit from another class if you want to use a mixin. However, you can simply inherit from Object:
class User extends Object with Persistence {
// implementation
}
But really, you can just inherit from Persistence as well which will have the same effect:
class User extends Persistance {
// implementation
}
Gilad Bracha explains that the syntax is specifically designed this way:
I think it is important to understand the semantic model here. "with"
is the mixin application operator, and it takes two parameters: a
superclass and a mixin, and yields a class. Saying "with Foo" in
isolation makes as much sense as saying >> 2 (you could interpret both
as curried functions, but that is very far from Dart). When you write
"C extends S with M", you are specifying a superclass following the
extends keyword, just as you do when you write "C extends K" except
that the superclass is not specified via an identifier but via a mixin
application. So the superclass would be "S with M".
As Lasse points out, as practical matter it doesn't restrict you, but
having the syntax reflect the underlying structure is important.

Related

What is the difference between 'implements' keyword and 'extends' keyword in dart?

I couldn't understand the difference between these two keywords.
By using extends we can get features from parent class. I think implements does that too.
First I thought the difference is overriding methods but with extends I can do that.(I might be wrong)
Is the difference of these two keywords about overriding methods or what? Thank you
extends means we get the implementation of a given class and we can then override members if we want our own implementation for certain variables or methods. You can also add new variables and methods.
implements means you get nothing from the class you implement from. But you promise that your class will be compatible with the interface of the class you are implementing. So no, you are not getting any implementation from the super class and you need to implement everything or declare your class abstract.

Error Mixin : Abstract classes cannot be instantiated in Dart

I was learning dart but it was still an error when I entered into the mixin, I don't know the fault where it is always an error when:
Abstract classes cannot be instantiated
I want to implement a mixin for Cat, Elang and Hiu with a subclass of Mamalia, Burung, Ikan
This is the Github code:
The error are rather clear. You cannot make an instance of an abstract class since an abstract class per definition is a class which can define methods and fields which are yet to be implemented and the class is therefore not complete.
The purpose of abstract classes is to let other classes extend from them and implement the missing methods.
I don't know why all your classes are marked as abstract but you can just remove the abstract keyword from the classes: Mamalia, Ikan and Burung and it should work since all of these classes are not needed to be abstract.
Abstract Class -
Use the abstract modifier to define an abstract class—a class that can’t be instantiated. Abstract classes are useful for defining interfaces, often with some implementation.
Don't instantiate Mamalia, Burung or Ikan classes. Instead instantiate Cat, Elang and Hiu.
Mamlia mamal = Cat(); // Will allow accessing methods defined only in Mamal class
Cat cat = Cat() // Will allow accessing all the methods defined in Cat class

"extends" versus "implements" versus "with"

I want to understand the difference between extends, implements and with. When do I use each keyword?
Extends:
Use extends to create a subclass, and super to refer to the superclass.
Extends is the typical OOP class inheritance. If class a extends class b all properties, variables, functions implemented in class b are also available in class a. Additionally you can override functions etc.
You use extend if you want to create a more specific version of a class. For example the class car could extend the class vehicle. In Dart a class can only extend one class.
Implements:
Every class implicitly defines an interface containing all the instance members of the class and of any interfaces it implements. If you want to create a class A that supports class B’s API without inheriting B’s implementation, class A should implement the B interface.
Implements can be used if you want to create your own implementation of another class or interface. When class a implements class b. All functions defined in class b must be implemented.
When you're implementing another class, you do not inherit code from the class. You only inherit the type. In Dart you can use the implements keyword with multiple classes or interfaces.
With (Mixins):
Mixins are a way of reusing a class’s code in multiple class hierarchies.
With is used to include Mixins. A mixin is a different type of structure, which can only be used with the keyword with.
They are used in Flutter to include common code snippets. A common used Mixin is the SingleTickerProviderStateMixin.
extend can only be used with a single class at the time, BUT... you can easily extend a class which extends another class which extends another class which...! ;)
In fact, most Flutter widgets are already built like that.

Mixins in dart 2.1 - "on" keyword

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"?
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.
What is the difference?
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

Is it possible to require generic type arguments on a Dart class?

A common question, specifically since Dart 2, is if it is possible to require some or all generic type arguments on some or all types - for example List<int> instead of List or MyType<Foo> instead of MyType.
It's not always clear what the intention is though - i.e. is this a matter of style (you/your team likes to see the types), to prevent bugs (omitting type arguments seems to cause more bugs for you/your team), or as a matter of contract (your library expects a type argument).
For example, on dart-misc, a user writes:
Basically, if I have this:
abstract class Mixin<T> {}
I don't have to specify the type:
// Works class Cls extends Object with Mixin<int> {} // ...also works
class Cls extends Object with Mixin {}
Is there some way to make the second one not allowed?
Strictly speaking, yes, and no.
If you want to enforce that type arguments are always used in your own projects (instead of relying on type inference or defaults), you can use optional linter rules such as always_specify_types. Do note this rule violates the official Dart style guide's recommendation of AVOID redundant type arguments on generic invocations in many cases.
If you want to enforce that generic type arguments are always used when the default would be confusing - such as List implicitly meaning List<dynamic>, no such lint exists yet - though we plan on adding this as a mode of the analyzer: https://github.com/dart-lang/sdk/issues/33119.
Both of the above recommendations will help yourself, but if you are creating a library for others to use, you might be asking if you can require a type argument to use your class. For example, from above:
abstract class Mixin<T> {}
abstract class Class extends Object with Mixin {}
The first thing you could do is add a default bounds to T:
// If T is omitted/not inferred, it defaults to num, not dynamic.
abstract class Mixin<T extends num> {}
If you want to allow anything but want to make it difficult to use your class/mixin when T is dynamic you could choose a different default bound, for example Object, or even better I recommend void:
In practice, I use void to mean “anything and I don’t care about the elements”
abstract class Mixin<T extends void> {
T value;
}
class Class extends Mixin {}
void main() {
var c = Class();
// Compile-time error: 'oops' isn't defined for the class 'void'.
c.value.oops();
}
(You could also use Object for this purpose)
If this is a class under your control, you could add an assertion that prevents the class from being used in a way you don't support or expect. For example:
class AlwaysSpecifyType<T> {
AlwaysSpecifyType() {
assert(T != dynamic);
}
}
Finally, you could write a custom lint or tool to disallow certain generic type arguments from being omitted, but that is likely the most amount of work, and if any of the previous approaches work for you, I'd strongly recommend those!

Resources