Angel \”Java\” Lopez on Blog

October 10, 2011

Dart Programming Language: Javascript killer?

Filed under: Dart, JavaScript, Programming Languages — ajlopez @ 10:48 am

Today was the day. Google new child, Dart programming languages, was unveiled. First, some links:

My previous post: Dart Programming Language: the new kid on the block
The announced Keynote at Goto Conference

Today Lars Bak post
Dart: a language for structured web programming
http://googlecode.blogspot.com/2011/10/dart-language-for-structured-web.html

Keynote Slides

Official Site
http://www.dartlang.org

The code
http://code.google.com/p/dart/

Hello World
http://www.dartlang.org/docs/getting-started/index.html

Language Specification
http://www.dartlang.org/docs/spec/index.html

Some news:

Google unveils Dart, its new programming language for building web applications
http://thenextweb.com/google/2011/10/10/google-unveils-dart-its-new-programming-language-for-building-web-applications/

Google starts to detail Dart
http://www.i-programmer.info/news/98-languages/3174-google-starts-to-detail-dart.html

Google debuts Dart, a JavaScript alternative
http://news.cnet.com/8301-30685_3-20117924-264/google-debuts-dart-a-javascript-alternative/

Now, some examples and comments. Simple language, this is the hello world:

main() { 
    var name = 'World'; 
    print('Hello, ${name}!');
}

Design goals (from Technical overview)

  • Create a structured yet flexible programming language for the web.
  • Make Dart feel familiar and natural to programmers and thus easy to learn.
  • Ensure that all Dart language constructs allow high performance and fast application startup.
  • Make Dart appropriate for the full range of devices on the web—including phones, tablets, laptops, and servers.
  • Provide tools that make Dart run fast across all major modern browsers.

At first glance, yes, it looks simple, familiar (it’s not Objective-C or APL ;-). The constructs are simple, but includes classes and inheritance. Static data types are optional. Notably, it can be run in two modes. See Lars Bak blog:

Dart code can be executed in two different ways: either on a native virtual machine or on top of a JavaScript engine by using a compiler that translates Dart code to JavaScript. This means you can write a web application in Dart and have it compiled and run on any modern browser. The Dart VM is not currently integrated in Chrome but we plan to explore this option.

Then, according to the Technical overview, problems to attack (web developers):

  • Small scripts often evolve into large web applications with no apparent structure—they’re hard to debug and difficult to maintain. In addition, these monolithic apps can’t be split up so that different teams can work on them independently. It’s difficult to be productive when a web application gets large.
  • Scripting languages are popular because their lightweight nature makes it easy to write code quickly. Generally, the contracts with other parts of an application are conveyed in comments rather than in the language structure itself. As a result, it’s difficult for someone other than the author to read and maintain a particular piece of code.
  • With existing languages, the developer is forced to make a choice between static and dynamic languages. Traditional static languages require heavyweight toolchains and a coding style that can feel inflexible and overly constrained.
  • Developers have not been able to create homogeneous systems that encompass both client and server, except for a few cases such as Node.js and Google Web Toolkit (GWT).
  • Different languages and formats entail context switches that are cumbersome and add complexity to the coding process.

Nice to see Node.js mentioned. Lars Bok wrote:

Dart targets a wide range of development scenarios: from a one-person project without much structure to a large-scale project needing formal types in the code to state programmer intent. To support this wide range of projects, Dart has optional types; this means you can start coding without types and add them later as needed. We believe Dart will be great for writing large web applications.

The language comes with a set of basic libraries and tools for checking, compiling, and running Dart code, all of which will evolve further with your participation. We’ve made the language and preliminary tools available as open source on dartlang.org. Check out the site to give feedback, learn more about Dart, and participate in its development.

An example with class, interface, types:

interface Shape {
  num perimeter();
}
class Rectangle implements Shape {
  final num height, width; 
  Rectangle(num this.height, num this.width);  // Compact constructor syntax.
  num perimeter() => 2*height + 2*width;       // Short function syntax.
}
class Square extends Rectangle {
  Square(num size) : super(size, size);
}

Notice the num type: Dart supports int and double as subtypes of num. See the Core Library (subject to change).

I could use the below code in my AjDraw javascript project ;-)

class Point {
  var x, y;
  Point(this.x, this.y);
  scale(factor) => new Point(x*factor, y*factor);
  distance() => Math.sqrt(x*x + y*y);
}
main() {
  var a = new Point(2,3).scale(10);
  print(a.distance());
}

See the => notation for function definition. No code at constructors? Yes, you can write it. Notably, there are named constructors:

class Point {
  num x, y;
  Point(this.x, this.y);
  Point.zero() : x = 0, y = 0;
  Point.polar(num theta, num radius) {
    x = Math.cos(theta) * radius;
    y = Math.sin(theta) * radius;
  }
}

More details at Idiomatic Dart: setter, getters in fields, factory constructors, function declaration, use of =>, string interpolation, equality (oh! it has == and ===).

Update: Isolate communication. There is no direct multithreading support in language (I guess the VM supports it). There are port in Isolates:

class Printer extends Isolate {
  main() {
    port.receive((message, replyTo) {
      if (message == null) port.close();
      else print(message);
    });
  }
}
main() {
  new Printer().spawn().then((port) {
    for (var message in ['Hello', 'from', 'other', 'isolate']) {
      port.send(message);  
    }
    port.send(null);
  });
}

First impresions: hmmm… it’s more clear than Javascript, but it could create a live ecosystem? browser adoption? At least, it can be compiled to Javascript. And apparently it’s easy to grasp.

My Links:

http://www.delicious.com/ajlopez/dart

It’s time to write a Dart interpreter in C#, and compile it to C#, leveraging CLR and .NET libraries. It’s DartSharp time! ;-) https://github.com/ajlopez/DartSharp

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

1 Comment »

  1. Amazing information about dart programming and for java script. An outstanding share! I have just forwarded this onto a colleague who had been doing a little research on this. And he actually ordered me breakfast simply because I discovered it for him… lol. So let me reword this…. Thank YOU for the meal!! But yeah, thanx for spending the time to talk about this subject here on your website.

    Comment by article writing — May 16, 2012 @ 3:17 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Shocking Blue Green Theme Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 56 other followers

%d bloggers like this: