@@ -687,57 +687,32 @@ If your class can be used as an interface, mention that in the class's doc
687
687
comment.
688
688
689
689
690
- ### DO use ` mixin ` to define a mixin type
690
+ ### PREFER defining a pure ` mixin ` or pure ` class ` to a ` mixin class `
691
691
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 >
726
693
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 >
729
695
730
696
{% include linter-rule-mention.md rule="prefer_mixin" %}
731
697
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.
737
707
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.
740
713
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.
741
716
742
717
## Constructors
743
718
0 commit comments