-
Notifications
You must be signed in to change notification settings - Fork 1.7k
prefer_mixin
lacks nuance
#58543
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
Comments
We make some exceptions for a few SDK classes
as a holdover until changes on the SDK-side let us handle this in a more principled way. It's not an awesome solution but we could consider adding @lrhn: I wonder if there are any updates relevant to this from the SDK or core lib perspective? |
I definitely would not want anything Flutter-related to be hard-coded into the linter in that way, because we want it to be just as possible for people to copy our framework and create the same product, and that won't work if the tools are hard-coded to a particular instance of the framework. My ideal solution here would be to just introduce some sort of annotation that we can add to all the classes mentioned above that just tells the lint to cool it on |
Sounds good. (If Here's a tracking issue on |
It's a little more verbose, but you could define both a mixin and a class that uses that mixin. Doing so would cover both of the use cases without too much extra code. It would also negate the need for an annotation. Perhaps something like mixin ChangeNotifier implements Listenable {
// ...
}
class SimpleChangeNotifier with ChangeNotifier {} I'm not objecting to an annotation, just making sure the alternatives have been considered.
It might be worth asking whether this is something that we'd want to use (and hence define) in the SDK. @lrhn For visibility. |
We've considered it, but I think having two classes makes the API harder to understand. In practice it would be six classes -- Listenable (the interface), ChangeNotifier (the class), ChangeNotifierMixin (the mixin), ValueListenable (the interface), ValueNotifier (the class), ValueNotifierMixin (the mixin), and that's before we start talking about Animation and so on. It's already pretty confusing as it is without adding more... |
I stand by dart-lang/language#33 (comment) The problem is migration. If someone currently I'm more inclined towards a language version based migration, like dart-lang/language#1643. |
I'm not anticipating us changing this API currently, but I'm open to the possibility that I'm just missing something important about how mixins work and that @lrhn is right that we should migrate. |
I want to migrate that to being a You can still use that as a mixin. You can still "extend" it by doing Maybe, just maybe, we should consider allowing Or we could allow a declaration like |
Short term (possibly even until Dart 3.0), the language team are fine with introducing an annotation like That could move us towards the point where existing classes used as mixins are actually made into mixin declarations, which might again allow us to make the breaking change to disallow mixing in classes before Dart 3.0. (Read: We do want this, but it's not urgent, and if we can stage the migration over time, using lints, it's easier than making a breaking language change and having to migrate "lots" of existing code.) We'll then have to figure out how to use annotations in the platform libraries too, since we also have some legacy mixins-declared-as-classes that we haven't dared migrating yet, but that's a separate issue. |
See also, dart-lang/language#1942. |
Hey ran into this when using Is there some kind of canonical solution to this? We should probably update the linter message to help users resolve this. I had a class like this class SomeModel extends ChangeNotifier with WidgetsBindingObserver { Which gave me the error: Prefer using mixins. See https://dart-lang.github.io/linter/lints/prefer_mixin.html and I converted it to this: class SomeModel with WidgetsBindingObserver, ChangeNotifier { but that still triggered the lint? Sorry I'm not a dart wizard so I don't really know what the correct solution should be here. I'm happy to just ignore the lint but it doesn't feel right. |
The issue here is that Reading the docs for
@goderbauer: any advice here? |
I don't have great advice, unfortunately. That lint doesn't work well with some of the concepts in the flutter framework that pre-dates mixins, see also dart-lang/core#743. However, it is totally fine to implement the WidgetsBindingObserver as @pq suggested. You'd probably just have to copy a lot of the empty stubs from the base classes to fully implement the interface. Or you can just ignore the lint with To really fix this, we'd need some changes to the dart languages, as discussed in the issues linked from ttps://github.com/dart-lang/lints/issues/26. |
With the |
Good deal. Thanks @goderbauer! |
The
prefer_mixin
lint points out uses ofwith
that mix in classes rather than mixins.Unfortunately, sometimes a class is intended to be used as both a mixin and a class, and there's currently no way to indicate that a particular class is therefore ok for use with
with
.Examples in Flutter's framework include
WidgetsBindingObserver
andChangeNotifier
.The text was updated successfully, but these errors were encountered: