
// named function
void sayGreeting(String salutation, String name) {
final greeting = '$salutation $name';
print(greeting);
}
// function with statement body
window.on.click.add((event) {
print('You clicked the window.');
})
// function with expression body
var items = [1, 2, 3, 4, 5];
var odd = items.filter((i) => i % 2 == 1);
print(odd); // [1, 3, 5]
class Point {
Point(this.x, this.y);
distanceTo(Point other) {
var dx = x - other.x;
var dy = y - other.y;
return Math.sqrt(dx * dx + dy * dy);
}
var x, y;
}
Class members:
operator +(other) => new Point(x+other.x, y+other.y);
num left, top, width, height;
num get right() => left + width;
set right(num value) => left = value - width;
Rectangle(this.left, this.top, this.width, this.height);Prefixing this. to parameters means that they will be automatically assigned to fields. This has been one of the proposed feature for JavaScript class literals.
Point.zero() : x = 0, y = 0;
factory Symbol(String name) {
if (_cache.containsKey(name)) {
return _cache[name];
} else {
const symbol = new Symbol._internal(name);
_cache[name] = symbol;
return symbol;
}
}
Symbol._internal(this.name);
interface Shape {
num perimeter();
}
class Rectangle implements Shape {
final num height, width;
Rectangle(num this.height, num this.width);
num perimeter() => 2*height + 2*width;
}
Factory classes for interfaces. An interface can specify a factory class which can be considered its default implementation. You can now directly instantiate the interface, its constructors delegate to the constructors of the factory class. If you use normal constructors then that is not much different from a normal class (but you don’t get the multiple inheritance of interfaces). Things become interesting with factory constructors, because you can create your own instance (including an instance of a subclass). I’ve seen interfaces being abused in Java code, to the point where there wasn’t a single class that didn’t implement an interface, so we’ll see where this goes. This feature does not provide mixin functionality. That is, if you implement an interface, you don’t get default method implementations via the factory class.
// Receive messages
class Printer extends Isolate {
main() {
port.receive((message, replyTo) {
if (message == null) port.close();
else print(message);
});
}
}
// Send messages
main() {
new Printer().spawn().then((port) {
for (var message in ['Hello', 'from', 'other', 'isolate']) {
port.send(message);
}
port.send(null);
});
}
Quoting the language specification:
Dart code is always single threaded. There is no shared-state concurrency in Dart. Concurrency is supported via actor-like entities called isolates. An isolate is a unit of concurrency. It has its own memory and its own thread of control. Isolates communicate by message passing. No state is ever shared between isolates. Isolates are created by spawning.
main() {
print(new List<String>() is List<Object>);
print(new List<Object>() is List<String>);
print(new List<String>() is List<int>);
print(new List<String>() is List);
print(new List() is List<String>);
}
var message = """
Dear $user!
There has been a problem!
""";
var foo = "world";
print("Hello $foo");
print("Result: ${x + y}");
66 folks listed as owners/commiters for Dart. Holy large team! http://code.google.com/p/dart/people/list@pmuellr:
Looks like Dart allows typedefs for functions, which should be nice for defining callback signatures.
I'll give it this, Dart certainly solves the problem of JavaScript not having anything to do with Java.@jordansissel, in a similar vein:
Dart: Putting the Java back in JavaScript.@jashkenas:
My favorite Dart compiler class: Hack.java@maccman:
Dart's source contains this little gem:
static bool isVm() { return 1234567890123456789 % 2 > 0; }
We are not targeting JavaScript, but rather fragmented mobile platforms.I’m not sure what that means. Will Dart target these platforms by compiling to JavaScript or by compiling to the native mobile environments? It almost makes you wonder whether Google really knows what to do with Dart.
Tentative pointers in that direction are Google not yet committing to integrating the Dart VM into Chrome and saying that Dart doesn’t target JavaScript, but a “fragmented mobile platform”. That is a marked change in tone from the openly anti-JavaScript document that was published last year [1]. And it is also a very weak message about what Dart actually is. Google will have to eventually give clear answers. By missing the opportunity to do so during today’s event, they have weakened the impact of the launch.
If you want to see a truly innovative language, take a look at Newspeak: Its elegant syntax has the well-known Smalltalk advantages and there are fresh new ideas regarding modularity and IDEs (check out the paper on Hopscotch). Gilad Bracha, one of its creators, is part of the Dart team, which is why I expected more from that language.