Pages

First Impressions: Dart (DartLang) 02 May, 2013


Dart is a language that can compile down to javascript. It also will make command line apps that run in a VM. It has some nice language features, including with being able to write javascript with compile time static type checks. It also includes DOM manipulation based on selectors. I might use this language for some tools.

Tools

Dart tools are pretty good. The Eclipse plugin had an issue, but the standalone Dart IDE worked great. I had no issues with the stand alone version's logging, warnings, errors, running, or auto-completion.


Speed

It's not fast.


Database Access

Fourth party support is needed for database drivers. By this I mean that DB vendors don't have Dart drivers and there is a small amount of support for third party support for the third party vendor drivers.

Intermingling Static and Dynamic Types

int a;
var b;

Implicit String Evaluation

int a = 5;
String b = "I have $a apples";

Underscores Control Visibility

//private:
int _i;
//public:
int i;

Everything is an Object

5.toString();
1 however is not == true

One Line Functions

//Instead of:
int add(int a, int b) {
  return a+b;
}
//You can do this:
int add(int a, int b) => a+b;

Function Parameters Can be Default / Optional

int add(int a, int b, [int c]) {
  if (?c) print(c); //c was passed
}add(a:6, b:2);

Closures

void main() {
  var f = add;
  f(2,3);
}
int add(int a, int b) {
  print(a+b);
}

Typecasting

Typecasting in Dart is a little different. You use the as keyword:
(someObject as Animal).eat();

Shorthand for Caller reuse

Meet the Cascade Operator.
class test {
  func1() {print("in 1");}
  func2() {print("in 2");}
  func3() {print("in 3");}
}
void main() {
  var t = new test();
  t..func1()
    ..func2()
    ..func3();
}

Easy Object Construction

Observe the absence of a constructor body, yet x gets set.
class test {
  int x;
  test(this.x);
}
void main() {
  var t = new test(6);
  print(t.x);
}

Constructor Delegation

Non-verbose constructor delegation.
SomeClass.myDefaultConstructor(x, y) : this(x, y, 0);

The Factory Keyword

There is a factory keyword to designate constructors that will not create new objects, even though you are using the new keyword when executing the constructor
factory Person(String name) {
  //Get a person from cache and return it
}

Implicit getters/setters

By default, getters and setters exist for all class variables. Extra ones can be created like this:
class Person {
  int baggageWeight;
  int personWeight;
  int get totalWeight => baggageWeight + personWeight;
}
void main() {
  Person p = new Person();
  int total = p.totalWeight;
}

Operator Overloading

A great return from C++.
class Person {
  int age;
  int operator +(Person p) {
    return this.age + p.age;
  }
}
void main() {
  Person p1 = new Person();
  Person p2 = new Person();
  p1.age = 20;
  p2.age = 30;
  int total = p1+p2;
  print(total);
}

Annotations

Dart annotations are easy to declare. They're just classes that usually start with lower case characters.
class asdf {}
@asdf
class Person{}

Name Spacing

You can use the as keyword to alias some package you've imported
import package:a/lib;
import package:b/lib as img;

The library and packaging system

So this is a little different than Java/C++ packages/headers
Libraries: The code part of a package that uses library and part keywords to modularize code for a package.
Packages: A package is a config + Library. The config lets you to define dependencies of a package using a Pubspec file (yaml).

Threads (Futures & Isolates)

Futures allow you to run callback after a Timer or thread(Isolate) has completed or erred out.
Isolates(Threads that are isolated from each other) are just functions passed to spawnFunction(). They can talk to other threads using send(), receive(), and call().
import 'dart:isolate';
import 'dart:async';
go1() {
  port.receive((msg, reply) {
    print ('i got $msg');
  });
}
Future go2(){
   Completer completer = new Completer();
   new Timer(new Duration(seconds:1), () {completer.complete("Time is up");});
   return completer.future;
}
void main() {
  var sendPort = spawnFunction(go1);
  sendPort.send("hello"); //Might not get processed if main exits too fast
  sendPort.call("a better way to send").then((reply) {print("sent");});
  go2().then((String result) {print(result);});
}