-
Notifications
You must be signed in to change notification settings - Fork 209
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
Having a mixin implement a private member of another mixin causes a name collision #4244
Comments
There has been repeated talk about making it an error for a mixin application with private member to override a concrete existing member with the same name. I don't remember whether it was ever decided to do so. I think it might have been. The analyzer appears to have implemented this: dart-lang/sdk#45959 (If the intent changes, but the spec doesn't, has the language really changed or not?) In any case, this is a perfect example of why you might want mixins to have the same private members as another class or mixin in the same library. If the base type is marked Here I don't know which precise rule is implemented. I do remember that one reason to disallow is to thereby allow implementations to assume (and optimize for) access to to a That does not make sense for an abstract method, so that should not be the reason to disallow this case. All in all, I think that error should be dropped. If we do want some error, we should not err when the mixin is declared We could also not err if one of the mixins super-interfaces contain the member, but then we'd have to check the mixin and superclass has a shared superinterface declaring the member. Then it's also "the same member", so it's not surprising to a compiler that there exists an implementation of that interface member in a mixin. |
What doesn't help here is that mixin composition isn't a thing. mixin OtherMixin with Mixin {
...
} |
@rrousselGit absolutely! Currently we have to either use multiple mixins or duplicate the logic. This includes the code in abstract mixin class Iterable<E> {
// This class has methods copied verbatim into:
// - SetMixin
// If changing a method here, also change other copies.
... |
I worked around it by using private top-level functions. Instead of: // mixin.dart
mixin Mixin {
int _method(int value);
void somePublicMethod() {
_method(42);
}
}
mixin OtherMixin on Mixin {
@override
int _method(int value) => value;
} I did: // mixin.dart
mixin Mixin {
void somePublicMethod();
}
void _somePublicMethodLogic(Mixin mixin, {required int Function(int) method}) {
_method(42);
}
mixin OtherMixin on Mixin {
int _method(int value) => value;
@override
void somePublicMethod() => _somePublicMethodLogic(this, method: _method);
} Not perfect, but that fixes the issue |
I actually wonder why mixins support private members at all. |
Consider the following:
Then:
This unexpectedly fails with:
But there's no name conflict. Instead
OtherMixin._method
is from the same interface asMixin._method
The text was updated successfully, but these errors were encountered: