Isolates
Isolates are another new feature of Dart. Experience has shown that shared memory threading is error prone and difficult to debug. To take advantage of multicore machines, isolates are provided to allow multiple and concurrent isolated memory heaps to run inside a single Dart application. Isolates communicate by passing messages over ports. These messages are copied before received, ensuring no state is shared.
#import('dart:isolate');
echo() {
port.receive((msg, SendPort replyTo) {
replyTo.send("Echo: $msg");
});
}
main() {
var echoService = spawnFunction(echo);
echoService.call("Hello!").then((answer) => print(answer));
}
Listing Two: How Dart uses isolates.
The code in this listing includes the following:
Line 01: Import the isolate library.
Line 10: Create an isolate for the echo function.
Line 11: Send a message, wait for a reply, print the reply.
Line 04: Receive messages to be echoed.
Use cases for isolates include safer Web app mash-ups, concurrency, and inter-application communication.
JavaScript Puzzlers Fixed by Dart
Dart also fixes some of JavaScript's more puzzling features. For example, if you are a JavaScript developer, you might know what the following code does. If you are not a JavaScript developer, you'll probably be surprised.
// JavaScript
var callbacks = [];
for (var i = 0; i < 2; i++) {
callbacks.push(function() {
console.log(i);
});
}
for (var x = 0; x < callbacks.length; x++) {
callbacks[x]();
This JavaScript code will print 2 and then 2.
This confusion is fixed in Dart. The following code prints 0 and then 1, as you would expect:
//Dart
main() {
var callbacks = [];
for (var i = 0; i < 2; i++) {
callbacks.add(() => print(i));
}
callbacks.forEach((c) => c());
}
Here's another example of surprising JavaScript behavior:
// JavaScript
> {} + []
0
This JavaScript snippet compiles and runs, and returns zero probably not what you'd expect.
The Dart Editor gives you an early warning that the code won't compile:

Figure 1.
When the code is run, instead of a confusing zero, Dart generates an error indicating where the problem is:

Figure 2.
Another surprising JavaScript feature is function binding and the scope of this. Consider the following code:
// JavaScript
function Awesome(button) {
button.addEventListener("click", function(e) {
this.cool(); // 'this' is actually the button, so it fails
});
}
Awesome.prototype.cool = function() {
window.alert("ice cold");
}
Thanks to Dart's lexical scopes, code is more reasonable and behaves more predictably.
// Dart
#import('dart:html');
class Awesome {
Awesome(Element button) {
button.on.click.add((e) => cool()); // Which cool?
// Awesome's cool,
// which is lexically scoped
}
cool() {
window.alert("ice cold");
}
}
Dart addresses more JavaScript puzzlers; this is just a sample. To be fair, ES.next (the next version of JavaScript) addresses some of these issues, but it's unclear when this new version will be supported by the majority of modern browsers. This situation means developers need to wait for the ES.next spec to be finished, as well as wait for ES.next and ES5 compilers to appear. Instead of waiting, Dart fixes these and offers a compiler for ES5.


