Skip to content

Commit c57c2c4

Browse files
Update Effective Dart on mixins for Dart 3 changes (#5017)
Fixes #4941
1 parent 8407c2d commit c57c2c4

File tree

2 files changed

+19
-65
lines changed

2 files changed

+19
-65
lines changed

examples/misc/lib/effective_dart/design_good.dart

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -523,27 +523,6 @@ class Graph1<Node, Edge> {
523523

524524
//----------------------------------------------------------------------------
525525

526-
class Control {}
527-
528-
// #docregion mixin
529-
mixin ClickableMixin implements Control {
530-
bool _isDown = false;
531-
532-
void click();
533-
534-
void mouseDown() {
535-
_isDown = true;
536-
}
537-
538-
void mouseUp() {
539-
if (_isDown) click();
540-
_isDown = false;
541-
}
542-
}
543-
// #enddocregion mixin
544-
545-
//----------------------------------------------------------------------------
546-
547526
// #docregion one-member-abstract-class
548527
typedef Predicate<E> = bool Function(E element);
549528
// #enddocregion one-member-abstract-class

src/effective-dart/design.md

Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -687,57 +687,32 @@ If your class can be used as an interface, mention that in the class's doc
687687
comment.
688688

689689

690-
### DO use `mixin` to define a mixin type
690+
### PREFER defining a pure `mixin` or pure `class` to a `mixin class`
691691

692-
{% include linter-rule-mention.md rule="prefer_mixin" %}
693-
694-
Dart originally didn't have a separate syntax for declaring a class intended to
695-
be mixed in to other classes. Instead, any class that met certain restrictions
696-
(no non-default constructor, no superclass, etc.) could be used as a mixin. This
697-
was confusing because the author of the class might not have intended it to be
698-
mixed in.
699-
700-
Dart 2.1.0 added a `mixin` keyword for explicitly declaring a mixin. Types
701-
created using that can *only* be used as mixins, and the language also ensures
702-
that your mixin stays within the restrictions. When defining a new type that you
703-
intend to be used as a mixin, use this syntax.
704-
705-
{:.good}
706-
<?code-excerpt "design_good.dart (mixin)"?>
707-
```dart
708-
mixin ClickableMixin implements Control {
709-
bool _isDown = false;
710-
711-
void click();
712-
713-
void mouseDown() {
714-
_isDown = true;
715-
}
716-
717-
void mouseUp() {
718-
if (_isDown) click();
719-
_isDown = false;
720-
}
721-
}
722-
```
723-
724-
You might still encounter older code using `class` to define mixins, but the new
725-
syntax is preferred.
692+
<a id="do-use-mixin-to-define-a-mixin-type"></a>
726693

727-
728-
### AVOID mixing in a type that isn't intended to be a mixin {#avoid-mixing-in-a-class-that-isnt-intended-to-be-a-mixin}
694+
<a id="avoid-mixing-in-a-class-that-isnt-intended-to-be-a-mixin"></a>
729695

730696
{% include linter-rule-mention.md rule="prefer_mixin" %}
731697

732-
For compatibility, Dart still allows you to mix in classes that aren't defined
733-
using `mixin`. However, that's risky. If the author of the class doesn't intend
734-
the class to be used as a mixin, they might change the class in a way that
735-
breaks the mixin restrictions. For example, if they add a constructor, your
736-
class will break.
698+
Dart previously (language version [2.12](/guides/language/evolution#dart-212)
699+
to [2.19](/guides/language/evolution#dart-219)) allowed any class that met certain restrictions
700+
(no non-default constructor, no superclass, etc.) to be mixed into other classes.
701+
This was confusing because the author of the class might not have intended it to
702+
be mixed in.
703+
704+
Dart 3.0.0 now requires that any type intended to be mixed into other classes,
705+
as well as treated as a normal class, must be explicitly declared as such with
706+
the `mixin class` declaration.
737707

738-
If the class doesn't have a doc comment or an obvious name like `IterableMixin`,
739-
assume you cannot mix in the class if it isn't declared using `mixin`.
708+
Types that need to be both a mixin and a class should be a rare case, however.
709+
The `mixin class` declaration is mostly meant to help migrate pre-3.0.0 classes
710+
being used as mixins to a more explicit declaration. New code should clearly
711+
define the behavior and intention of its declarations by using only pure `mixin`
712+
or pure `class` declarations, and avoid the ambiguity of mixin classes.
740713

714+
Read [Migrating classes as mixins](/language/class-modifiers-for-apis#migrating-classes-as-mixins)
715+
for more guidance on `mixin` and `mixin class` declarations.
741716

742717
## Constructors
743718

0 commit comments

Comments
 (0)