The Basics of Google’s Dart Milestone 1

Dart LanguageA year on from its initial release, Google Dart’s Milestone 1 has a whole raft of improvements and new features, though the removal of the string concatenation for performance and consistency reasons has left a few scratching their heads. It’s OK, though. You can also use StringBuffer, string interpolation (see below) to concatenate string literals.

What isn’t in doubt is that Dart is starting to gain traction, having recently entered the Tiobe Index Top 50 and is currently at the 43rd spot — just above ActionScript!

Dart is Google’s JavaScript killer and is a language that runs in the browser — though just Chrome for now. The refusal of other browser creators to embrace Dart has left Google slightly out on a limb. If Dart can leave JavaScript behind in terms of performance and ease of use by developers, then those same browser creators may have to bite the bullet and adopt it or get left behind.

My previous article, JavaScript or Dart, looked at this from a higher level. Now let’s get technical now and see what the differences really are.

JavaScript is a dynamically typed language, which means the type checking is done at runtime. Static type checking can be done before the program runs, which should make it more reliable and, more important, faster. The compiler can generate code that doesn’t need to verify the type at runtime.

Dart gives you the option of dynamic or static typing. Typing is optional so you can declare a variable using var (like JavaScript) and have the type:

[sourcecode language="javascript"]
var a=’David’;

int a= 7;
a =’Fred’; // Error !
[/sourcecode]

Dart’s Objects

As one who comes from the world of class based objects in C++, Delphi, C# and Java, I’ve struggled a bit with the classless JavaScript “objects,” I for one welcome our new Dart objects. They’re more classy.

You aren’t tied to declaring everything in classes in Dart, though. Functions, variables and even getters and setters can be declared at the top level.

Earlier, I mentioned string interpolation to overcome the removal of + for string concatenation. Use the $ prefix to expand variables inside strings (not unlike PHP).

[sourcecode language="javascript"]
var name = ‘David’;
var title = ‘My Name is’;
var greeting = ‘$title: $name’;
[/sourcecode]

No prizes for guessing that greeting now holds ‘My Name is: David’. Who needs +?

Tools

To use Dart currently, you have to run Dart2js to generate JavaScript. But there’s now a version of Chromium (the open source browser behind Chrome) called Dartium that directly runs Dart. Or you can use the open source Dart Editor as well (it’s cross platform on Windows, Mac and Linux). That lets you develop Dart programs, edit, run, and debug. If you specify a browser to run it in, it will even compile Dartto JavaScript for you.

I have to say that one of the things that lets Dart down is the spec. The Dart team should take a leaf out of the Golang (Google Go) website and the spec of Go there. It’s better laid out, easier and much less tedious to read.

I’ll be increasing Dart coverage of Dart here soon with lessons on how to get started and use it.

Related Links

Comments

  1. BY googlesethladd says:

    Hi,

    Just to be clear, Dart compiles to JavaScript, so it runs in much more than Chrome. Also, we don’t want to kill JavaScript (that’s impossible anyway), we just want to make a productive development experience for developers from all over.

    Thanks for writing about Dart!

  2. BY googlesethladd says:

    Sorry, not sure if my comment was eaten or not.

    I wanted to clarify two points:

    1) Dart compiles to JavaScript, so it’s not just for Chrome. Dart runs in many modern browsers.

    2) We don’t want to kill JavaScript (that’s impossible). We want to help developers from all platforms be productive as they build awesome apps for the modern web.

    Thanks for writing about Dart!

Post a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>