Search code examples
syntaxdartmixins

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.


Solution

  • 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.