Dart: An Introduction
Florian Loitsch / Riviera DEV, 20.10.2011
“I guarantee you that Apple and Microsoft (...) will never embed the Dart VM. So 'Works best in Chrome' and even 'Works only in Chrome' are new norms promulgated intentionally by Google.”
— Brendan Eich
main() {
print('Hello World');
}
const THE_ANSWER = 42;
print('half of the answer is ${THE_ANSWER / 2}');
var list = [1, 2, 3];
list.forEach((e) => print(e));
var map = {
'foo' : 12
};
map['bar'] = 13;
map.forEach((k, v) => print('$k * 2 = ${v * 2}'));
concat(a, b) => a + b;
var foobar = concat('foo', 'bar');
/// the superhero class
class Superhero {
String name;
Superhero(this.name);
toString() => 'I am $name!';
}
main() {
Superhero batman = new Superhero('Batman');
print(batman);
}
Optional Types in Dart / Why Dart Types Are Optional and Unsound
// convention
void publicMethod(String something) {
var now = new DateTime.now();
}
true is truthynull – no undefined
class Superhero extends Person with SuperPower {
String name;
String _realName;
Superhero.withRealName(this._realName);
String get realName => _realName;
set realName(realName) => _realName = realName is String ? realName : 'UNKNOWN';
void train({times: 1}) { }
Team operator +(Superhero other) => new Team([this, other]);
}
main() {
var superman = new Superhero.withRealName("Clark Kent")..name = "Superman";
superman.train(times: 4);
}
class Cache {
static final Cache _INSTANCE = new Cache._internal();
static Map<String, String> _store;
factory Cache() {
if (_store == null) {
_store = <String, String>{};
}
return _INSTANCE;
}
Cache._internal();
}
var cache = new Cache();
library testlib;
import 'dart:math' as m show PI;
import 'package:bootjack/bootjack.dart' hide Alert;
part '04_testlib_part.dart';
“There are two candidates for the TIOBE programming language of the year award that will be announced around the 1st of January 2015. These are statistical language R (+1.38%) and Apple's new Swift language (+1.06%). Apart from these, also JavaScript and Dart have a chance to win.”