A BRIEF INTRODUCTION TO
DART
By Randal L. Schwartz <merlyn@stonehenge.com>
Version 1.3 on 10 July 2015
OVERVIEW
Not just a "smarter javascript"
TypeScript
CoffeeScript
ES6
Modern language
Native VM for
development
"node.js" server­side code
supporting #!/path/to/dart scripts
Transpiles efficiently to JavaScript
Great for single page applications
Runs in all modern browsers
DART...
is easy to learn
compiles to JavaScript
runs in the client and on the server
has great tools
supports types, without requiring them
scales from small scripts to large
has a wide array of built­in libraries
supports safe, simple concurrency with isolates
supports code sharing
is open source
HISTORY
Founded by Lars Bak and Kasper Lund of Google
Unveiled in October 2011
Dart 1.0 released November 2013
Approved as ECMA­408 standard in July 2014
Dart 1.6 (deferred loading) released August 2014
Dart running on Google Cloud in November 2014
"Dart VM in every browser" replaced with "better
JS" in March 2015
Dart 1.9 (enum and async) released March 2015
On­the­fly code analysis tools released March 2015
Dartpad announced May 2015
First dart developer summit in April 2015
Third edition of the spec published June 2015
Dartpad!
EDITORS
Webstorm (no cost to approved students,
classrooms, and open source projects)
IntelliJ Community Edition (open source)
Sublime Text (proprietary)
Dartpad (zero install!)
Eclipse (open source)
Or... it's just text... use your favorite editor!
But then you miss out on the code analyzer
tools...
HELLO, WORLD!
Of course... the first code...
Let's introduce a variable...
That's an untyped variable... let's ensure it's a
string...
main() {
print("Hello, world!");
}
main() {
var msg = "Hello, world!";
print(msg);
}
main() {
String person = "World";
print("Hello, $person!");
}
HELLO, PEOPLE!
Let's take it from the command line...
And refactor that into a subroutine...
main(List<String> args) {
String person = "World";
if (args.length > 0) {
person = args[0];
}
print("Hello, $person!");
}
main(List<String> args) {
String person = "World";
if (args.length > 0) {
person = args[0];
}
sayHelloTo(person);
}
sayHelloTo(String aPerson) {
print("Hello, $aPerson!");
}
IMPORTANT CONCEPTS
Everything is an object (inheriting from Object
class)
Typing is optional, but provides hints to the tools
Typing also provides guidance in "checked" mode
Code is completely compiled before execution
Includes top­level, local, and anonymous
functions, as well as class and instance methods
Privacy is two level: public and private (underscore
prefix)
BUILT­IN TYPES
Numbers (int and double)
String (String, UTF­16 internally)
Booleans (type bool, only values are true and false)
List (indexed starting at 0)
Maps (indexed by objects... yes objects)
Symbol (string that is identical if it is equal)
NUMBERS
Infinite precision ints in VM, normal range in
JavaScript
Doubles are classic IEEE 754 standard
Converting between strings and numbers:
int a = 3;
int header_constant = 0xBABE;
int large = 31415926535897932384626433832795;
double pi_like = large / 1e31;
main () => print(pi_like);
int three = int.parse("3");
double pi_kinda = double.parse("3.14");
String six_string = 6.toString();
String pi_sorta = pi_kinda.toString();
main () => print(pi_sorta);
BOOLEANS
Only two literals: true and false
Only true is true. Everything else is false
... unless you're in JavaScript, then it's normal JS
rules
When developing, you can ensure proper
operation before deploying
If you debug in checked mode, booleans must be
true or false
LISTS
Also known as arrays or ordered collections
Zero­based indexing (first element is item 0)
In checked mode (during development), can be
optionally typed:
Includes rich set of methods: add, addAll,
indexOf, removeAt, clear, sort and many
more
main () {
var meats = ['cow', 'pig', 'chicken'];
print(meats);
print(meats[0]);
meats.add('turkey');
print(meats[meats.length - 1]);
var only_numbers = <num> [3, 4, 5];
only_numbers.add('duck'); // fails in checked mode
}
MAPS
Maps have keys and values, both objects
Maps are created with a literal syntax or new Map
Typing information can be added for keys and
values:
main() {
var randal = {
'name': 'Randal L. Schwartz',
'address': '123 Main Street',
'city': 'Portland, Oregon',
};
print(randal['city']);
randal['city'] = 'Beaverton, Oregon';
randal['zip'] = 97001;
}
main() {
var randal = <String,String> {
...
FUNCTIONS
Types are optional, but are checked if present and
assist IDE:
Args can be positional, or named:
void sayMyName (String myName) {
print("My name is $myName.");
}
sayMyNameToo (myName) => print("My name is $myName.");
main () => sayMyName("Randal");
void sayMyName({String first, String last}) {
if (last != null) {
print("My name is $first $last");
} else {
print("My name is $first");
}
}
main () {
sayMyName(first: "Randal");
sayMyName(first: "Randal", last: "Schwartz");
}
OPTIONAL ARGS
Required args listed first, followed by optional args
Optional args can be positional or named, but not
both
Optional args can have defaults, or return null
otherwise:
void sayMyName(String first, [String last]) {
if (last != null) {
print("My name is $first $last");
} else {
print("My name is $first");
}
}
void sayMyNameToo(String first, [String last = "Smith"]) {
print("My name is $first $last");
}
main () {
sayMyName("Randal", "Schwartz");
sayMyNameToo("Randal");
}
OPERATORS
Rich set of operators similar to most modern
languages
15 levels of precedence, with normal usage of
parentheses to override
Integer divide: ~/
Pre and post increment and decrement
Type testing and casting: is, is! and as
Compound assignment: a += 3
Bitwise operators, like & and <<
Expression if/then/else:
var $this = 3 > 4 ? "was true" : "was false";
main () => print($this);
CONTROL FLOW
Generally very similar to C or JavaScript
if and else
for loops
while and do-while loops
switch and case
assert (throw exception if false in checked mode)
EXCEPTIONS
Unlike Java, no need to declare types you will
throw or catch
Exceptions typically subclass from Error or
Exception
But you can throw any object (which is everything)
Standard try, catch, finally sequence
CLASSES
Everything is an instance of a class, ultimately
inheriting from Object
Single inheritance with mixins
The new keyword can be used with a classname or
a class method
Objects have members consisting of methods and
instance variables
Classes can be abstract, typically defining a mixin
interface
Many operators can be overridden to provide a
custom interface
Extend a class with the extends keyword
CLASS MEMBERS
The dot is used to refer to both methods and
instance variables:
Getters and setters can be defined for instance
variables, allowing transparent migration
Class variables and methods also supported
class Point {
num x;
num y;
String toString() => "Point [$x,$y]";
}
main() {
var point = new Point();
point.x = 4;
point.y = 7;
print(point);
}
GENERICS
Optional types, so why use generics?
Inform the compiler during checked mode, and
the IDE during development:
Can also be used for interfaces:
main() {
var names = new List<String>();
names.addAll(['Randal', 'Kermit']);
names.add(1e20); // fails because 1e20 is not a String
}
abstract class Cache<T> {
T operator [] (String key);
operator []= (String key, T value);
}
ASYNCHRONOUS OPERATIONS
Similar to promise libraries in JavaScript
Provides Future (one value soon) and Stream
(many values over time) interfaces
Can use try/catch/finally­style logic with either of
these
For simple cases, async can be made to appear
non­async:
checkVersion() async {
var version = await lookUpVersion();
if (version == expectedVersion) {
// Do something.
} else {
// Do something else.
}
}
LIBRARIES
dart:core ­ numbers, collections,
strings, and more
dart:async ­ asyncronous
programming
dart:math ­ math and random
dart:html ­ browser­based apps
dart:io ­ I/O for command­line
apps
dart:convert ­ decoding and
encoding JSON, UTF­8, and more
dart:mirrors ­ reflection
... and many more standard
libraries
but wait... there's even more...
PUB
Like Perl's CPAN for Dart
Publish shared libraries
Incorporate those libraries into
your project
The pubspec.yaml controls
acceptable version numbers
Other users can view your
dependencies when they pull down
your pubspec.yaml
REFERENCES
Many parts of this talk inspired by
these sources:
Almost everything is linked from
Dart wikipedia page
Dart: Up and running
Dart news
dartlang.org

A brief introduction to dart