Variables – to type or not to type
In our first example (Raising rabbits) in Chapter 1, Dart – A Modern Web Programming Language, we started by declaring a variable rabbitCount
dynamically with var
, and then in the second version we gave it a static type int
(refer to the file prorabbits_v1.dart
and prorabbits_v2.dart
in Chapter 1, Dart – A Modern Web Programming Language) and concluded that typing is optional in Dart. This seems confusing and has provoked a lot of discussion: "is Dart a dynamic language like Ruby or JavaScript, or a static language like Java or C#?" After all, some of us were raised in the (static) belief that typed variables are absolutely necessary to check if a program is correct, a task mainly performed by the compiler (but the Dart VM has no separate compiler step, and dart2js, the Dart to JS compiler, doesn't check types because JS is fully dynamic).
It turns out that no mainstream language actually has a perfect type system (static types don't guarantee program correctness...