-
Notifications
You must be signed in to change notification settings - Fork 1.7k
unable to infer non nullable type in case of generics #56954
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
Summary: The issue is that Dart's type inference fails to recognize that a generic type parameter |
This is working as intended. You expect We do actually support a special case: If you change the name of Another possible way ahead would be to adopt 'stable getters', dart-lang/language#1518, which would establish a guarantee that a certain getter will return the same value if you call it multiple times. However, that's a proposal rather than an actual language mechanism, so we can't do that today. (You can vote for it, though ;-) |
ok thanks! seems about right. final class A {
final int? value;
A(this.value);
}
void main() {
int? n = 3;
int n2 = n == null ? 3 : n + 2; // as intended
if (n != null) {
int n2 = n; // as intended
}
A a = A(3);
int b = a.value == null
? 3
: a.value +
2; // error Operator '+' cannot be called on 'int?' because it is potentially null
if (a.value != null) {
int b = a.value; // error
}
} |
// --- Library 'a.dart'.
final class A1 {
final int value;
A1(this.value);
}
base class A2 extends A1 {
A2(super.value);
}
// --- Library 'b.dart'.
import 'a.dart';
var _sillyCounter = 0;
base class B extends A2 {
B(): super(0);
int get value => ++_sillyCounter;
}
// --- Library 'main.dart'.
import 'a.dart';
import 'b.dart';
void main() {
A1 a = B();
print('The value is ${a.value}, then ${a.value}');
} Just like promotion, superinterface relationships also have special privileges in the same library (in this case: a subclass of a |
In these cases we should be able to infer that the type cannot be null, however the current implementation raises a compile error:
The text was updated successfully, but these errors were encountered: