-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Lint on "covariant <Function>"? #57742
Comments
However, here's an example where it is used, and no other solution is available: abstract class A { void foo(covariant Function f); }
class B1 implements A { void foo(Function({int i}) f) {}}
class B2 implements A { void foo(Function(int) f) {}} The function types used to annotate On the other hand, if the problem is simply that As an aside: Currently, invocations of a function of type The approach where we use local static information to choose an argument type is interesting, but only safe "one level down": // Could be an instance method, but the issue comes up
// even for a top-level function.
void registerCallback<V>(void Function(V) onChange) {
_actualCallback = (dynamic value) => onChange(value);
// `as V` needed if `--no-implicit-casts`.
}
void Function(dynamic) _actualCallback;
class A<X> { void Function(X) callback; }
main() {
A<num> a = A((int i) => print("Hello, $i!"));
registerCallback(a.callback);
} We will call We could introduce support for invariance ( Alternatively, we could introduce covariance for parameters of arbitrary functions: registerCallback((covariant SomeType x) => baz(x)); The function literal would yield a function object of type But the main point still stands: The proper type for a void function which must be unary, but the argument type can be anything (which is then checked dynamically) is |
OK. It sounds like it worth requesting a lint for this then? |
I think you're right: There are cases where |
Thanks! Do you think it makes sense to put this in the /cc @srawlins @natebosch @leafpetersen too. |
It's a little bit like an instance field |
It is my impression that there's nothing wrong with Should we have a lint that flags every |
I no longer feel passionate about this issue :). I think it was a bigger issue when migrating to Dart 2. |
I've started to see users to try and write the following:
The problem with this code, is that, unless you are very careful, it is almost always wrong (see examples 1, 2, 3). The reason I've seen users try and write this is because they start with:
... and due to
dynamic
no longer being bottom, it fails statically (covariant Function
fails at runtime). The "correct" code is the hardest to remember, but I'm hoping we can also figure out a solution for that (i.e. a lint/hint/style guide change):DartPad example showing the following 3 cases:
https://dartpad.dartlang.org/9c0b30f65e8cf84d3107bdc8d23c8dcf
(Is it ever valid/intended to write
covariant Function
?)The text was updated successfully, but these errors were encountered: