class A {
int b;
int c;
A(this.b, this.c);
}
void testFunc(Object param) {
print((param as A).b);
print(param.c); //is there documentation somewhere? Why is there no need type casting?
}
Why is there at second time no need type casting? I need ref to documentation.
2
Answers
Dart is smart
Dart analyzer logically decides which type this object is, generally this cast expression has 2 possible scenarios:
param
is an instance of A class or a subtype of it.TypeError
So, you will get an error that indicates that
param
is not of typeA
, say you caught that error : the next expression ofparam.c
will cause an error indicating thatparam
doesn’t have a getter calledc
.after all, the successful cast scenario most likely to answer your question.
The Dart code analyzer remembers the type for a local variable (such as your
param
). Since the first line intestFunc
already makes it clear thatparam
is anA
rather than just anObject
, it knows that for the rest of the function too.Note that this applies only to local variables and parameters, as more global value might actually change between lines.