YOU ARE DOWNLOADING DOCUMENT

Please tick the box to continue:

Transcript
Page 1: Google Dart

27.10.11

Google Dart Eberhard Wolff Architecture & Technology Manager adesso AG

Page 2: Google Dart

Dart: A Structure Web Programming Language ►  New programming language

►  New programming tools

►  New open source project

►  Currently in a preview

►  …for feedback

27.10.11

Page 3: Google Dart

The Team Behind Dart ►  Lars Bak

>  Beta language >  HotSpot Java VM >  V8 JavaScript VM in Google Chrome >  18 software patents

►  Gilad Bracha >  Computational Theologist and later Distinguished Engineer at

Sun >  Java Language Specification >  Java Virtual Machine Specification

►  Both worked on Strongtalk (Smalltalk + static typing)

27.10.11 3

Page 4: Google Dart

Why Dart? ►  More and more web application with complex logic

►  Will become a lot more: HTML5 on the rise

►  So far: No really great languages designed to create large scale web applications

►  Google: The competition is NOT JavaScript ... but fragmented mobile platforms

►  GWT (Google Web Toolkit) already featured a Java to JavaScript compiler

►  Dart is designed with simplicity and mass appeal in mind

27.10.11 4

Page 5: Google Dart

Runtime Environment ►  Dart has its own VM

►  Open Source project

►  Needed for some advanced features

►  Dart can compile into JavaScript

►  Runs on any browser

►  Currently not very efficient

►  i.e. about the same performance as first V8 releases

►  Size of JavaScript code currently considerable

27.10.11 5

Page 6: Google Dart

Hello World in Dart

►  C like language

27.10.11

main() { print('Hello, Dart!');}

int fib(int n) { if (n <= 1) return n; return fib(n - 1) + fib(n - 2);}main() { print('fib(20) = ${fib(20)}');}

►  Seemingly static typing

Page 7: Google Dart

Objects and Classes

►  Easy to initialize fields

27.10.11 7

class Person { String name; Person(this.name);}main() { Person p = new Person('Gilad'); print('Hi ${p.name} ');}

Page 8: Google Dart

Objects and Classes

►  No method or constructor overloading

27.10.11 8

class Person { String name; String firstname; Person(this.name); Person.withFirstname(this.firstname,this.name);}main() { Person p = new Person.withFirstname('Gilad','Bracha'); print('Hi ${p.firstname} ${p.name}');}

Page 9: Google Dart

Namespaces ►  Much like Java packages

►  Might contain classes, interfaces, variables and functions

►  Definition:

#library("http");

►  Using:

#import("http.dart");

►  Optional prefix:

#import("http.dart”,”http”);

27.10.11 9

Page 10: Google Dart

Dart Library

27.10.11 10

Page 11: Google Dart

Object Construction With Factories

►  Very elegant approach to allow for other object creation algorithms

27.10.11 11

interface Person factory PersonFactory { Person(name); final name;}class PersonFactory { factory Person(name) { if (name == 'Wolff') { return new Adessi(name); } return new RealPerson(name); }}

class Adessi implements Person { Adessi(this.name); String name;}class RealPerson implements Person { RealPerson(this.name); String name;}main() { Person p = new Person('Wolff'); print(p is Adessi); p = new Person('Bracha'); print(p is Adessi);}

Page 12: Google Dart

More Complex Example

27.10.11 12

class Person {}class Customer extends Person { buy() {print("bought");}}main() { Person p = new Customer(); p.buy();}

Page 13: Google Dart

More Complex Example

►  Code actually compiles and runs

►  There are no type errors – only Static Warnings

►  Types are considered annotations

►  Type annotations never change the semantics of a program 27.10.11 13

class Person {}class Customer extends Person { buy() {print("bought");}}main() { Person p = new Customer(); p.buy();}

Page 14: Google Dart

On Types ►  Type theory: A type defines a set of values and operations on them

>  i.e.Java int: values: all integers from -2147483648 to 2147483647 >  Operations on int: + - * etc

►  Goal: Check types to find errors >  i.e. multiply a String by an int >  i.e. call a method that does not exist on an object

►  Type checkers proof the partial correctness of programs

►  i.e. at least types are correct

►  Typing will only find basic errors

27.10.11 14

Page 15: Google Dart

Dynamic Typing ►  At runtime

►  Advantage >  Simpler >  More flexible >  Can even implement methods on the fly – Meta programming

►  Disadvantage >  Perceived as less secure

27.10.11 15

Page 16: Google Dart

Static Typing ►  Typing checked At compile time

►  Advantage >  IDE support >  Documentation >  Perceived as safer >  …but problems should be found by Unit Tests, too!

►  Disadvantage >  Overhead if no type inference >  Complex to get right in some case >  Complex (Java Generic FAQ by Anglika Langer is 297 pages !)

27.10.11 16

Page 17: Google Dart

Generics in Java ►  Does this code compile?

27.10.11 17

public class Customer extends Person {…}public static void main(String[] args) { List<Person> listOfPerson = new ArrayList<Person>(); List<Customer> listOfCustomer = new ArrayList<Customer>(); listOfCustomer=listOfPerson; listOfPerson=listOfCustomer;}

Page 18: Google Dart

Generics in Java ►  Does this code compile?

27.10.11 18

public class Customer extends Person {…}public static void main(String[] args) { List<Person> listOfPerson = new ArrayList<Person>(); List<Customer> listOfCustomer = new ArrayList<Customer>(); listOfCustomer=listOfPerson; listOfPerson=listOfCustomer;}

listOfPerson.add(new Person());listOfCustomer.add(new Person());Customer c = listOfCustomer.get(1);Customer c = listOfPerson.get(1);

Page 19: Google Dart

Generics and Static Typing in Java ►  You can still mess with it

►  Basically each type cast disables static typing

►  i.e. it can introduce problems that otherwise would have been discovered by the compiler

27.10.11 19

listOfPerson.add(new Person());Object obj = listOfPerson;listOfCustomer = (List<Customer>) obj;Customer c = listOfCustomer.get(0);

Page 20: Google Dart

Static vs. Dynamic Typing: My Take ►  Errors found by a static type checker will also be found by unit tests

►  The security of static typing is only perceived

►  Real advantage: Documentation

►  “I expect you to pass in a Customer!”

►  “This gives you a Person!”

►  Tool support

►  Easier to come up with suggestions for content assist

►  However, Dynamic languages tools are catching up

27.10.11 20

Page 21: Google Dart

Generics in Dart

27.10.11 21

class Person {}class Customer extends Person { buy() {print("bought");}}main() { List<Customer> listOfCustomer = new List<Customer>(); List<Person> listOfPerson = listOfCustomer; listOfPerson.add(new Person()); Customer c = listOfCustomer[0]; c.buy();}

Page 22: Google Dart

Generics in Dart

►  Call to buy() won’t work

►  Problem not found until the method is actually called

►  Optional run time type checker would find the problem one line earlier

►  Valid code: List<Customer> is a List<Person>

27.10.11 22

class Person {}class Customer extends Person { buy() {print("bought");}}main() { List<Customer> listOfCustomer = new List<Customer>(); List<Person> listOfPerson = listOfCustomer; listOfPerson.add(new Person()); Customer c = listOfCustomer[0]; c.buy();}

Page 23: Google Dart

Generics in Dart ►  Generics in Dart are considered covariant

►  i.e. List<Customer> is List<Person>

►  This is in fact logical incorrect (see last slide)

►  But: >  Do you want to read 297 pages Generic FAQ? >  And: It is just a help to spot basic errors

27.10.11 23

Page 24: Google Dart

Concurrency

►  What is the problem?

27.10.11 24

public class MyCache { Map<Integer, Integer> map = new HashMap<Integer, Integer>(); public void add(int i, int j) { map.put(i, j); }

public int read(int i) { return map.get(i); }}

Page 25: Google Dart

Concurrency

►  What is the problem?

►  Code is not thread safe!

►  Should use ConcurrentHashMap instead

27.10.11 25

public class MyCache { Map<Integer, Integer> map = new HashMap<Integer, Integer>(); public void add(int i, int j) { map.put(i, j); }

public int read(int i) { return map.get(i); }}

Page 26: Google Dart

Concurrency ►  What is the real problem?

►  Concurrency in object-oriented systems

►  OO is based on concurrent access shared state >  Hard to get correct >  If you get it correct: performance bad >  Locks >  Synchronization >  Etc

►  Pretty low level concept

►  This is why Carnegie Mellon dropped OO from the Computer Science curriculum!

►  Can we do better?

27.10.11 26

Object with State

Threads

Page 27: Google Dart

Dart code is always single threaded!

27.10.11 27

Page 28: Google Dart

Isolates ►  Inspired by Actor model

►  As present in Erlang

►  As done in Scala with Actors and Akka

►  Idea: State is hidden in Isolates

►  Isolates communicate with messages send to ports

►  Each isolate only works on one message at a time

►  No concurrency issues in an Isolate: Only one thread

►  Still concurrent: Multiple isolates might be active at a time

27.10.11 28

Isolate with State

Port

Page 29: Google Dart

Isolates

27.10.11 29

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); });}

Page 30: Google Dart

More Fun With Isolates ►  Isolates might be mapped to Web Worker in JavaScript

►  Nothing is shared

►  Isolates will have their own heap on the Dart VM >  Messages must be serialized and deserialized >  Garbage Collection can happen per Isolate >  No more stop-the-world GC!

►  Isolates might be used to introduce security concepts >  Enforce security rules on ports >  To replace Same Origin Policy

27.10.11 30

Page 31: Google Dart

More Fun With Isolates ►  Isolates require a light weight concurrency model

>  OS Thread switches take too much time >  In Erlang 100.000 of Isolates equivalent are not uncommon >  No way to run that many threads >  Interesting issue for Dart VM

►  Isolates for Remoting >  Can send messages to a port on a different host >  Semantics don’t change as serialization is done anyway

►  Probably more high level concurrency models

►  Specifications says that Isolates might run different versions of libraries

27.10.11 31

Page 32: Google Dart

Even More Fun With Isolates ►  In Erlang the concept is extended for high availability

>  Links allow to listen to events of Isolates >  Supervisor can monitor other processes >  If an error occurs the isolate crashes and is restarted >  Hierarchies of supervisors can be created >  State is a problem here >  This makes high availability easy to develop >  Dart might come up with a similar solution

27.10.11 32

Page 33: Google Dart

Snapshots ►  VM can be snapshot

►  Current heap etc

►  Faster startup >  No need to create initial state, config, … >  Used in V8 already for standard JavaScript libraries >  How long does a JVM need to start up?

►  Possible answer to mobile applications killed due to memory constraints

►  Could be used to migrate isolates between machines

27.10.11 33

Page 34: Google Dart

Dart Editor ►  Editor for Dart applications

►  Based on Eclipse

►  Code in the Open Source Project

27.10.11 34

Page 35: Google Dart

More Complex Example ►  Swarm: A newsreader

►  Completely written in Dart >  App code: 3210 LOC >  UI library code: 13200 LOC >  Animation yields 30 fps >  Runs on iPad and Chrome

27.10.11 35

Page 36: Google Dart

Not Decided Yet… ►  Currently a technology preview

►  Some questions are open >  Reflection? Probably using Mirrors >  Changing classes on the fly? Probably not needed due to Isolates >  What about Chrome?

27.10.11 36

Page 37: Google Dart

Links ►  http://www.dartlang.org/

>  Language Site >  Tutorials >  Language Spec >  Library Spec >  …

►  http://dart.googlecode.com/ >  Open Source project >  Including compiler, VM etc.

►  http://code.google.com/p/jdart/ >  Dart on the JVM

►  http://it-republik.de/jaxenter/artikel/Google-Dart-4121.html >  German

►  http://jonaswesterlund.se/dart.html >  Pre compiled Dart for Mac OS X

►  http://www.infoq.com/articles/google-dart 27.10.11 37

Page 38: Google Dart

Possible Applications for Dart ►  Google App Engine

>  Isolates are a nice fit for GAE’s restriction >  Snapshot will make it easy to move execution from

machine to machine

►  Android >  As a replacement for Java

►  Web Browser >  Compiling into JavaScript

27.10.11 38

Page 39: Google Dart

Dart: Conclusion – Huge Potential ►  Best shot at next generation web language so far

►  Language designed to appeal to the masses

►  Solves a real problem: Complex web applications

►  Already possible to create cross platform web applications

►  Google has a lot of resources and this solves a real problem for them

►  Isolates and future concurrency models very interesting >  Client: Security >  Server: Modern concurrency >  …

27.10.11 39

Page 40: Google Dart

27.10.11 40


Related Documents