You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As of Dart 2.12.0, the following code results in a static type error:
voidmain() {
final nums = [1, 2.0, 3.5];
final ints = [1, 2, 3];
final doubles = [1.1, 2.2, 3.3];
sums(nums);
sums(ints);
sums(doubles);
}
voidsums<Textendsnum>(List<T> list) {
T res = list[0];
for (var i =1; i < list.length; i++) {
res = res + list[i];
}
print(res);
}
main.dart:15:15: Error: A value of type 'num' can't be assigned to a variable of type 'T'.
res = res + list[i];
^
The same code when run in Dart 2.10.5 compiles successfully and prints the following:
6.5
6
6.6
When the sums method is altered to include the suggested fix, the error goes away in Dart 2.12.0:
voidsums<Textendsnum>(List<T> list) {
T res = list[0];
for (var i =1; i < list.length; i++) {
res = res + list[i] asT;
}
print(res);
}
But when it is changed to use an addition-assignment operator, the error persists regardless of the cast. However, the static analysis error is hidden in the IDE (VS Code, DartPad) by a warning that suggests the cast is unnecessary:
voidsums<Textendsnum>(List<T> list) {
T res = list[0];
for (var i =1; i < list.length; i++) {
res += list[i] asT; // IDE warning, error on `dart analyze` or on compile
}
print(res);
}
Both of these amended versions run flawlessly in Dart 2.10.5, with or without the cast.
This behavior has been confirmed on Mac OS 11.2.3 in VS Code (Dart extension 3.20.1) as well as the null-safety version of DartPad in Chrome.
The text was updated successfully, but these errors were encountered:
Abion47
changed the title
Type inference of List parameter in method with restricted type parameter resulting in analysis error
Bug: Type inference of List parameter in method with restricted type parameter resulting in analysis error
Mar 20, 2021
The difference is that res = res + list[i] as T; means res = (res + list[i]) as T;, whereas res += list[i] as T; means res = res + (list[i] as T);. The latter cast is unnecessary since list[i] already has type T.
The error comes from res having type T extends num, so all we know about res + something is that it returns num (that's what num.operator+ does, and all we know about T is that it implements num).
However, num is not assignable to T extends num without an implicit or explicit downcast, and Null Safety removed the implicit downcast. So, you need to cast the result.
Dart has added some special cases to the typing of int.+, and similar operators, to avoid errors like this in cases where you're just doing integer arithmetic. Those special cases do not extend to T extends num.
I'm creating this issue in response to a question that was asked today on StackOverflow.
As of Dart 2.12.0, the following code results in a static type error:
The same code when run in Dart 2.10.5 compiles successfully and prints the following:
When the
sums
method is altered to include the suggested fix, the error goes away in Dart 2.12.0:But when it is changed to use an addition-assignment operator, the error persists regardless of the cast. However, the static analysis error is hidden in the IDE (VS Code, DartPad) by a warning that suggests the cast is unnecessary:
Both of these amended versions run flawlessly in Dart 2.10.5, with or without the cast.
This behavior has been confirmed on Mac OS 11.2.3 in VS Code (Dart extension 3.20.1) as well as the null-safety version of DartPad in Chrome.
The text was updated successfully, but these errors were encountered: