Channels ▼
RSS

Web Development

Dart: Build HTML5 Apps Fast


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.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 
Dr. Dobb's TV