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
For function scoped dynamic variables the type is inferred inside a block that checks the type. For example
if (dynamicVar isString) {
// dynamicVar is inferred to be String
}
However this doesn't work for class fields as in the following example:
classTest {
dynamic classVariable;
List<String> stringList;
voidcheckClassVariable() {
if (classVariable isString) {
// ERROR: Could not infer type parameter E, dynamic must be of type String. ([untitled1] bin/main.dart:7)
stringList = [classVariable];
}
}
voidcheckMethodVariable() {
dynamic methodVariable;
if (methodVariable isString) {
// Type is inferred
stringList = [methodVariable];
}
}
}
The text was updated successfully, but these errors were encountered:
The feature is called type promotion in the language specification, and it is specified to apply to local variables and function/method parameters, but not instance variables, static variables, nor library variables (aka global variables).
The reason why this feature does not generalize to those "more global" kinds of variables is that it is based on an analysis which ensures that the given variable keeps the same value between the type test and the usage. Since arbitrary code can access those "more global" variables (including instance variables, if the enclosing object is accessible), it is much more complex (and in general it is an undecidable question) whether the variable is mutated between those two points in time. So the language avoids diving into that tar pit.
For function scoped dynamic variables the type is inferred inside a block that checks the type. For example
However this doesn't work for class fields as in the following example:
The text was updated successfully, but these errors were encountered: