24
London Dart

Dart London hackathon

Embed Size (px)

DESCRIPTION

Slides from the London Dart Hackathon

Citation preview

Page 1: Dart  London hackathon

London Dart Hackathon

Page 2: Dart  London hackathon

Technical Preview…What you see today may change tomorrow…

Enable us (as users of Dart) to influence the language and provide feedback.

Page 3: Dart  London hackathon

Why Dart?Complex applicationsTeam developmentEasily “toolable”

//process a & b. function process(a,b) { return a+b;    };

process(1,2,3);    process("Hello","World");    process({'name:'objectA'}, {'name':'objectB'})); 

All valid, but which is correct?

JavaScript

Page 4: Dart  London hackathon

main() { var anInt = 1; var aStr = "String"; var anObj = new Object(); var result = doSomething(anInt,aStr,anObj);}

doSomething(a,b,c) { return "blah";}

Design goals - FamiliarBe Familiar

Page 5: Dart  London hackathon

Design goals - FamiliarBe Familiar

void main() { int anInt = 1; String aStr = "String"; Object anObj = new Object(); String result = doSomething(anInt,aStr,anObj);}

String doSomething(int a, String b, Object c) { return "blah";}

Page 6: Dart  London hackathon

Design Goals - PerformancePerformance as a feature

Dart VM was faster at launch than V8 (chrome's JavaScript engine) was at launch.

Converted JS should be as fast as or faster than equivalent hand written JavaScript.

Page 7: Dart  London hackathon

Dartium (Chromium with Dart VM)

Page 8: Dart  London hackathon

Dart Editor

Page 9: Dart  London hackathon

dart2js: Dart to JavaScript#import('dart:html');

class MyApp { MyApp() { } void run() { write("Hello World!"); }

void write(String message) { document.query('#status').innerHTML = message; }}

void main() { new MyApp().run();}

Page 10: Dart  London hackathon

dart2js:the output JavaScript//...snip library code...// ********** Code for MyApp **************function MyApp() {}

MyApp.prototype.run = function() { this.write("Hello World!");}

MyApp.prototype.write = function(message) { get$$document().query("#status").innerHTML = message;}

// ********** Code for top level **************function main() { new MyApp().run();}

Page 11: Dart  London hackathon

Quick Demo…Dart Editor and Dartium

Page 12: Dart  London hackathon

Simple function syntaxvar myFunc = (a,b) { return a,b;}

var myFunc = (a,b) => a+b;myFunc(a,b) => a+b;doSomething(c,d, myFunc);doSomething(c,d, (a,b) => a+b);var result = myFunc(101,202);

Dart: FunctionsDifferent

syntax, same effect

Functions as

arguments

Anonymous function

Unsurprising function

call

Page 13: Dart  London hackathon

Dart: Classes and interfacesFamiliar (to Java and C# developers)But a couple of nice featuresclass Duck implements Quackable { var colour;

Duck([this.colour="red"]) { } Duck.yellow() { this.colour = "yellow"; }

String sayQuack() => "quack";}

//Usagevar duck1 = new Duck();var duck2 = new Duck("blue");var duck3 = new Duck.yellow();print(duck3.sayQuack());

Optional paramters

Named constructors

Function shorthand

Unsurprising this

Page 14: Dart  London hackathon

Dart: Classes and interfacesFamiliar (to Java and C# developers)But a couple of nice features

interface Quackable default Duck { String sayQuack();}

//Usagevar duck1 = new Quackable();

Default Implementati

on

Page 15: Dart  London hackathon

Dart: Classes and interfacesAll classes are also interfaces

class Person implements Duck { … }Class properties can be interchanged with getters and setters

duck.colour = "yellow"; //setter, or property?

class Duck { var _colour; //private property get colour() => _colour; //getter set colour(value) { //setter _colour=value; }}

Page 16: Dart  London hackathon

Dart: Libraries and PrivacyBreak up single source code file into multiple,

independent files.

Break logical parts of an app into libraries.Import your own and third party libraries.Privacy declarations apply at a library level

(not a class level)

#library("myLibrary");

#import("./libs/otherLib.dart");

#source("./myFile1.dart");#source("./myFile2.dart");

Page 17: Dart  London hackathon

Dart: Optional TypesAdd documentation to code

Documentation readable by humans and tools

"Innocent until proven guilty"

Type annotations have no effect on the running applicationvar i = 1;var s = "Hello";

int i = 1;String s = "Hello";

String i = 1;int s = "Hello";Probably wrong, but

not proved to be wrong.

Page 18: Dart  London hackathon

Dart: Optional TypesOptional types can be useful when prototyping an app

class Person { sayQuack(){ return "ouch…quack"; }}

pokeDuck(duck) { duck.sayQuack();}

//UsagepokeDuck(new Duck());pokeDuck(new Person()); //runs fine

But is that what the library designer intended?

Page 19: Dart  London hackathon

Dart: Optional TypesBut as you add structure, types can help

you… class Person { sayQuack(){ return "ouch…quack"; }}

pokeDuck(duck) { duck.sayQuack(); duck.swimAway();}

//UsagepokeDuck(new Duck());pokeDuck(new Person()); //throws exception

This now fails with a

noSuchMethod exception

New method added to

Duck, which isn't in Person

Page 20: Dart  London hackathon

Dart: Optional TypesAdding type info provides documentation to tools and

humans.class Person { sayQuack(){ return "ouch…quack"; }}

pokeDuck(Duck duck) { duck.sayQuack(); duck.swimAway();}

//UsagepokeDuck(new Duck());pokeDuck(new Person()); //tools warn

Now the tools can provide warnings

(or errors in checked mode).

Type information

Page 21: Dart  London hackathon

Dart: noSuchMethodAll classes can have a noSuchMethod method

(inherited from the base Object class)

class Person { sayQuack(){ return "ouch…quack"; }

noSuchMethod(name, args) { if (name == "swimAway") { throw "I'm not really a duck"; } }}

Similar to ruby's method_missing

Side note: Any object can be thrown as an

exception

Page 22: Dart  London hackathon

Libraries: dart:html, dart:jsonClient side library for interacting with the

DOMUses Dart constructs for DOM manipulation

var foo = elem.query("#foo"); //return a foo

var foos = elem.queryAll(".foo"); //list of foo

Events: foo.on.click.add((event) { //do something});

Autocomplete HTML5JSON Parsing of Maps, Lists

Page 23: Dart  London hackathon

Libraries: dart:ioServer side libraries:

Processes File IOSocketsHttp Client & Server

Page 24: Dart  London hackathon

More to do and time to try itwww.dartlang.org #dartlang

www.html5rocks.com