- int
- String
Type checking
We can declare the types of variables, but that does not make any difference during normal run-time. We can still assign values of other types to the variable, and it sill only recognizes real type-mismatch in use at run-time.
examples/dart-intro/type_checking.dart
void main() { var a = 23; print(a); a = "hello"; print(a); int b = 42; print(b); b = "abc"; print(b); String c = "19"; int d = 23; print(c + d); }
dart type_checking.dart (production mode)
23 hello 42 abc Unhandled exception: Illegal argument(s): 23 #0 _StringBase.+ (dart:core-patch/string_patch.dart:89) #1 main (file:///.../dart/examples/intro/type_checking.dart:15:13) #2 _startIsolate.isolateStartHandler (dart:isolate-patch/isolate_patch.dart:216) #3 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:115)
dart --checked type_checking.dart (checked mode)
23 hello 42 Unhandled exception: type 'String' is not a subtype of type 'int' of 'b'. #0 main (file:///.../dart/examples/intro/type_checking.dart:9:9) #1 _startIsolate.isolateStartHandler (dart:isolate-patch/isolate_patch.dart:216) #2 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:115)
IDE
The IDE will put little warning marks both of the above lines, and if we run it in the IDE we'll get an error at the same line where the --checked version go the exception.
Breaking on exception: type 'String' is not a subtype of type 'int' of 'b'.
Dart is an optionally typed and dynamic language. Variables can be
- annotated with static types
- untyped (aka. dynamic)