First thoughts on Dart from a GWT developer
Posted by David Chandler on October 10, 2011
I consider myself more of a tools & frameworks guy than a language geek so I’m more likely to groan than gush over a new language for the Web.
However, as I dug into Dart a bit working on a couple of the samples, I have to say I caught a bit of the same fire that I felt when I first encountered Google App Engine and GWT. Dart is fun. Coming from a GWT / Java background, I found it easy to get started.
Here are a few of my favorite things about Dart so far:
- Closures. You can register an event handler as simply as
element.on.click.add((Event e) { window.alert('You clicked'); });
Already, I don’t miss the clutter of anonymous inner classes. - Functions can be passed by reference. You can define
myClickHandler(Event e) { ... }
and then just write
button.on.click.add(myClickHandler);
- Function type aliases are a compact way to create your own callback types.
- Optional typing. At first I thought I wouldn’t like this. However, as the Ruby community has long pointed out, Java is full of redundancy. Why write
String[] values = new String[] {"a","b","c"};
when
values = ['a','b','c'];
will do? - Getters and setters. If you define methods using the get or set keywords, Dart will invoke them when you reference a field using dot notation. That is,
some.property
automatically invokessome.getProperty();
. - Constructor initializers. If an argument has
this.
before it in a constructor argument list, the field with that name will automatically be initialized with that argument’s value. So a constructor that simply assigns arguments to fields can be written on one line:
Point(this.x, this.y);
- The Dart DOM and HTML libraries provide convenient and mostly transparent access to the DOM. Thus, you can easily use static layout in an HTML file and call document.getElementById(). The Canvas code in the Sunflower and Spirodraw samples is almost identical to the corresponding Javascript.
- document.query(id_or_class) and .queryAll() work very similar to jQuery. These are part of the Dart HTML library, which offers convenient access to the DOM.
Thanks to all the syntactic sugar, Dart code is quite a bit smaller than the corresponding GWT app. My GWT sunflower app required very little modification to run in Dart, and the Dart code for the app went from 96 lines to 58 lines. Better still, I replaced the GWT SliderBar widget (almost 1000 lines of Java) with just a few lines of static HTML (my apologies to Firefox users: FF doesn’t support <input type=”range”> yet). Spirodraw was reduced from ~500 lines to ~300 lines, and the control panel layout is completely static HTML, which makes it easier to read.
Of course, it’s very, very early for Dart. GWT is a mature toolkit with a solid IDE (Google Plugin for Eclipse, among others), mature frameworks for RPC, MVP, etc., and a large open source community. But based on first looks, Dart is a promising new way to write Web apps. If browser vendors implement the Dart VM natively, it will rock! Chrome seems pretty likely to do this, at least. Until then, you can use dartc to produce JS for any modern browser. Hopefully, the editor (demo’d in the GOTO conf keynote this morning) will ship soon and make it easy for everyone to get started with Dart.
See also
- Official Dart language site
- Dart code samples (including ported GWT samples Sunflower and Spirodraw)
- Idiomatic Dart (great intro to Dart features)
Raymond Cidad Moser said
How do you see the future with dart and gwt?
Does this mean that google is gonna put all the eggs in Dart?
Can this be the start of an end for gwt?
thanks for the posts.
David Chandler said
Hi Raymond, it’s much too early to discern what may happen. Google uses GWT in over a thousand projects internally, whereas Dart is brand new. Google continues to invest in GWT and will continue to support it for a long time to come because it is used so widely within the company. So no, Google is certainly not putting all its eggs in the Dart basket. Whether it’s the beginning of the end for GWT is a bit like asking if any given day on the stock market is the beginning of a new bull or bear market. It depends greatly on how the Web programming community receives Dart.
StrongSteve said
Seems like a nice new language.
But I share your opinion that it will take some time to become as mature as GWT is today.
Personally I hope GWT will not vanish but integrate Dart! π
Jeff Larsen (@larsenje) said
When I saw the samples for Dart I knew you had a hand in writing some of them π
ARMistice said
Thanks David,
for the great Demos, and your view on Dart. Do you know where I can find an API for the dart:dom library? Or something similiar?
David Chandler said
Thanks. Google engineer Bob Nystrom just published an article on the Dart DOM, in fact: http://www.dartlang.org/articles/improving-the-dom/
Otherwise, your best bet is to browse the source of the dom and html libs here: https://code.google.com/p/dart/source/browse/branches/bleeding_edge/dart/client/
Hugh said
97 % of your favorite things about Dart already apply to JavaScript + HTML + CSS.
David Chandler said
Why yes, some of my favorite things about Dart are also some of my favorite things about Javascript. That’s why I like Dart–it allows me to use some of those features without having to hold my nose at the lack of explicit typing and accompanying lack of tool support.
David Welch said
Google’s an extremely large company with many different moving parts. It’s unlikely that they’ll put all their baskets in any one thing (meaning GWT I don’t think would be abandoned anytime soon)
Glad to see Dart though. It looks like what I’ve been hoping for over the past few years: a fluid, easy to work with language with a fair amount of type safety and static-language-benefits like compile time errors, good tooling (like code-complete), etc…
Thanks for the summary