-
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
Generic function type mismatch: type '(Subtype) => ReturnType' is not a subtype of type '((Supertype) => ReturnType)?'
#55286
Comments
I think the problem is that the |
Correct. The occurrence of final num Function(T)? method; is contravariant, while the Because of that, the code that reads the property Basically it makes the read expression into Which then fails at runtime, as intended. If it hadn't thrown there, the next statement could've been If you change the final num Function(T) _method;
num method(T value) => _method(value); then the reading becomes type safe, because the reader is now using the same type for There is just no way to allow an actual |
Thank you for the clarification. Anyway, I did something similar with manual casting: final num Function(T) _method;
num method(A value) => _method(value as T); But your solution is better. |
See dart-lang/language#297 for some background info about the treatment of occurrences of type variables in positions that disagree with their variance. See dart-lang/language#524 for a proposal about statically checked variance (covariance, contravariance, invariance). If we get that then you can use this: class B<in T extends A> {
const B({this.method});
final num Function(T)? method;
} .. and then |
Output:
The text was updated successfully, but these errors were encountered: