Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
Suggest using getters in mixins #4767
Changes from 1 commit
d4c9403
f7d8931
d1acde6
0f4e770
76fcba7
b882601
32a4756
84689fc
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
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 comment
The 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
on
directive specifying the superclass that defines whatever it is the mixin relies on (or theabstract
way to accomplish the same thing, explained in this comment and (kind of) in the new section I added after this PR was created).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.
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:
I think this is probably the kind of code that @KyleFin has in mind. (If not, correct me. :) ). In this example,
Person
is the subclass ofNameIdentity
.So there's no need for an
on
clause onNameIdentity
. (The rule foron
clauses is simple: If your mixin doesn't have anysuper
calls, you don't need anon
clause.) Instead, all you need is an abstract member declaration. In this case, it'sString get name;
.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 comment
The 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
on
.I believe my misunderstanding about which is the super/sub-class came from the current wording
you can restrict a mixin's use by using the on keyword to specify the required superclass
. @munificent's comment clarifies the strange behavior ofon
. I agree it shouldn't be highlighted in the language tour, but maybe there should be some description somewhere since it's a keyword? Maybe a link to these comment threads? Maybe theon
keyword could be marked as deprecated or something to limit incorrect usage?Maybe
on
could be omitted from this mixin documentation but there could beon (exception)
andon (mixin)
entries in https://dart.dev/language/keywords, similar tofinal (variable)
andfinal (class)
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.
It's not deprecated because it does have intended uses. They're just rare. You need an
on
clause when you want to have asuper
call inside the mixin, like:The
on
clause exists to define the type thatsuper
calls are resolved against. In the example here, theon SomeBaseClass
clause is what the compiler uses to know thatsuper.baseClassMethod()
is OK.