Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dart mixin 'with' cannot be used without 'extends'?

I'm working in Webstorm 6.0.2 and getting an error when trying to use mixin syntax:

class A{}

class B with A{} //error can't use with syntax without an extends?

Why can't I use with without extends? Surely every class implicitly extends Object.

like image 997
Daniel Robinson Avatar asked Jun 20 '13 08:06

Daniel Robinson


People also ask

How do you use mixin in darts?

A mixin is a class with methods and properties utilized by other classes in Dart. It is a way to reuse code and write code clean. Mixins, in other words, are regular classes from which we can grab methods (or variables) without having to extend them. To accomplish this, we use the with keyword.

Which keyword is used for the mixin to implement in a class in Dart?

Mixins in Dart are a way of using the code of a class again in multiple class hierarchies. We make use of the with keyword followed by one or more mixins names.

What is the difference between a mixin and inheritance?

Mixins are sometimes described as being "included" rather than "inherited". In short, the key difference from an inheritance is that mix-ins does NOT need to have a "is-a" relationship like in inheritance. From the implementation point of view, you can think it as an interface with implementations.

What is difference between extend and implement in flutter?

If class Second extends class First all properties, variables, methods implemented in class First are also available in Second class. Additionally, you can override methods. You use extend if you want to create a more specific version of a class.


1 Answers

Here's a really clear explanation from Ladislav Thon :

[...] there's a simple advice, which is actually semantically correct: in the declaration class C extends SC with M1, M2, M3 implements I1, I2 { ... }, imagine parenthesis around the content of the extends clause. They will look like this: class C extends (SC with M1, M2, M3) implements I1, I2 { ... }. Which means that class C doesn't extend SC, it extends SC_with_M1_with_M2_with_M3.

Or, put another way: the class declaration has an extends clause and an implements clause, but it doesn't have a with clause. Instead, the with clause belongs inside the extends clause.

And another point from Florian Loitsch :

When you extend "Object" with a mixin the first mixin can always take the place of "Object".

So your class B with A should be class B extends Object with A that is also equivalent to class B extends A.

like image 122
Alexandre Ardhuin Avatar answered Oct 02 '22 17:10

Alexandre Ardhuin