-
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
[Type Error] (int) => void
is not a subtype of type (dynamic) => void
#53523
Comments
This is a corner case of how function types interact with covariance in Dart. Consider function_arg<dynamic> arg = function_arg<int>("arg", (int v) { }, 123);
arg.register("whatever");
To work around the issue you need to shift invocation to a context where type is reified, e.g. class function_arg<T> {
void invoke() { register(value); }
} |
Agreeing with @mraleph's explanation, I'd like to mention a broader perspective on this program: Introducing a method like However, the declaration of So if you really need to have an instance variable whose type is We do not currently have direct support for that. However, we could change the language to support it by adding support for declaration-site variance, dart-lang/language#524. (@gintominto5329, if you want to support this feature, you can vote for that issue). If you're interested, here is a brief explanation about how that would work. With declaration-site variance, we could declare // Assuming `--enable-experiment=variance`.
class function_arg<inout T> { // <-- `T` is changed to `inout T`. No other changes needed.
const function_arg(
this.id,
this.register,
this.value,
);
final String id;
final void Function(T) register;
final T value;
} The modifier This implies that we get a compile-time error at the location where we previously laid the foundation for the run-time error:
If you want to get this kind of type checking now, where declaration-site variance hasn't yet been added to the language, you can emulate it as follows: typedef function_arg<T> = _function_arg<T, Function(T)>;
class _function_arg<T, Invariance> {
const _function_arg(
this.id,
this.register,
this.value,
);
final String id;
final void Function(T) register;
final T value;
} This implies that you cannot have a |
hello,
We need to know, whenever some functions are called, along with the passed arguments.
Note: Whole testing is done, on Dartpad, with Master channel
The following error, is being output, by then following code
Error
Complete output
Code
Code notes
Record
type is un-suitable, because of its alphabetic sorting of members, we need them in specified order, otherwise its perfect.print
s are just for reproduction of the issue, please do not become stack-overflow, regarding best practicesthanks
The text was updated successfully, but these errors were encountered: