-
Notifications
You must be signed in to change notification settings - Fork 715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Suggest using getters in mixins #4767
Changes from 2 commits
d4c9403
f7d8931
d1acde6
0f4e770
76fcba7
b882601
32a4756
84689fc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,9 +51,9 @@ mixin Musical { | |
} | ||
``` | ||
|
||
Sometimes you might want to restrict the types that can use a mixin. | ||
For example, the mixin might depend on being able to invoke a method | ||
that the mixin doesn't define. | ||
Sometimes the mixin might depend on being able to invoke a method or access fields | ||
that the mixin doesn't define. To require classes that use the mixin to define these, | ||
you can define getters in the mixin or restrict the types that can use a mixin. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is meant by "restrict the types that can use a mixin"? I'm curious how a mixin restricts types that use it, and feel this point should probably be expanded somewhere. PTAL @munificent There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not Bob 🙃 but I think that wording puts the emphasis on the wrong part of the point its trying to make. It's the wording that is currently on the page, it's not new to this PR:
The point though, isn't restricting the types that can use it, but ensuring that any methods the mixin might rely on that are defined by another type are in fact actually defined before the mixin can be used. I.e. that the mixin can only be used with the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The initial PR comment says:
But I think this is a (very common) misunderstanding of the inheritance order that mixins give you. What you most often want is to be able to access state in the mixin's subclass. For example: /// Can be applied to any type with a [name] property and provides an
/// implementation of [hashCode] and operator `==` in terms of it.
mixin NameIdentity {
String get name;
int get hashCode => name.hashCode;
bool operator ==(other) => other is NameIdentity && name == other.name;
}
class Person with NameIdentity {
final String name;
Person(this.name);
} I think this is probably the kind of code that @KyleFin has in mind. (If not, correct me. :) ). In this example, So there's no need for an I agree with @KyleFin that users often don't realize they can have mixins that access state on the class they are applied to by calling getters which are defined as abstract on the mixin. I like the idea of adding a little emphasis about that to the docs. It's valuable because the restriction that mixins can't have their own fields with initializers can make them harder to use without knowing this technique. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes @munificent's example with an abstract getter is what I had in mind. Thanks for the excellent context! I agree with the plan to rewrite the examples, removing emphasis on I believe my misunderstanding about which is the super/sub-class came from the current wording Maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It's not deprecated because it does have intended uses. They're just rare. You need an class SomeBaseClass {
baseClassMethod() {
print('Base class!');
}
}
mixin SomeMixin on SomeBaseClass {
mixinMethod() {
print('Calling superclass method!');
super.baseClassMethod(); // <--
}
}
class SomeDerivedClass extends SomeBaseClass with SomeMixin {}
main() {
SomeDerivedclass().mixinMethod();
}
The |
||
As the following example shows, you can restrict a mixin's use | ||
by using the `on` keyword to specify the required superclass: | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know that this is important to include in the docs, but this is where I was getting stuck with the existing example from the docs.