1 of 40

Google Dart language

2 of 40

What is Dart Language ?

Dart : A Structure web programming language

It’s a :

*New Open source project

*New programming language

*New programming tools

3 of 40

The team Behind Dart

1/ Lars Back

>18 software patents

>V8 JavaScript VM in Google chrome

>HotSpot Java VM

>HotSpot Java VM

4 of 40

The team Behind Dart

2/ Gilad bracha

> Computational theologist and later Distinguished Engineer at sun

> Java language specification

>Java virtual machine specification

5 of 40

Why Dart

6 of 40

>more and more application with complex logic.

>will become a lot more : HTML5 on the rise.

>so far:No really great language designed to create large scale web application.

>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.

7 of 40

The Dart language is familiar and easy to learn.

It's class based and object oriented,

without being dogmatic.

All modern browsers (both desktop and mobile) can run Dart web apps, thanks to our Dart-to-JavaScript compiler.

Core libraries provide all the basics, including support for asynchronous programming with Futures.

8 of 40

Tools such as Dart Editor and the pub package manager keep you productive.

Performance is good and getting better. Dart apps are fastest in the Dart VM, but they can be speedy even after compilation to JavaScript.

Polymer.dart lets you use future web APIs today, with support for web components, custom elements, data binding, and more.

9 of 40

The Code : Syntax

Dart aims to be unsurprising, but it does add a few language features to keep your code succinct yet understandable.

10 of 40

import 'dart:math'; // Import a library.// Functions and variables can be inside or outside of classes.var shapes = new List(); // A global variable.�addShape(shape) => shapes.add(shape); // Function shorthand syntax.// Every app has a main() function, where execution starts.main() { � // The cascade operator (..) saves you from repetitive typing.�

11 of 40

addShape(new Ellipse(10, 20)..rotation = 45*PI/180� ..color = 'rgb(0,129,198)'� ..outlineWidth = 0);� � // You can easily insert expression values into strings.� print('Area of the first shape: ${shapes[0].area}');�}

12 of 40

Hi,Dart

Basic web app with the obligatory "Hello, world" example.

13 of 40

HI.DART

library hi;

import 'dart:html';

main() {

query('#status').text = 'Hi, Dart';

}

14 of 40

HI.HTML

<!DOCTYPE html>

<html>

<head>

<title>Hi Dart</title>

</head>

<body>

<h2 id="status">Waiting for Dart to start</h2>

<script type="application/dart" src="hi.dart"></script>

<!-- The preferred way to reference dart.js is to add a Pub dependency to the 'browser' package and src 'packages/browser/dart.js'. →

<script src="https://dart.googlecode.com/svn/branches/bleeding_edge/dart/pkg/browser/lib/dart.js"></script>

</body>

</html>

15 of 40

Optional Types:

Declaring types is optional in Dart. These types don't affect the runtime behavior, but you can use them to clarify APIs and get better help from tools—for example, better code completion, easier navigation, and earlier error detection.

16 of 40

// Without types.�recalculate(origin, offset, estimate) {� // ...�}

// With types (and an optional parameter).num recalculate(Point origin,� num offset,� {bool estimate:false}) {� // ...�}

17 of 40

Classes

Dart is object oriented, with full support for classes. Each class has a single superclass, but classes can implement any number of interfaces.

18 of 40

import 'dart:math';class Ellipse extends Shape { // Declare a class.� num majorAxis = 0; // An instance variable (property).� num minorAxis = 0;� static const num C = PI/4; // A constant.� num get area => C*majorAxis*minorAxis; // A property implemented with a getter.�� Ellipse(this.majorAxis, this.minorAxis); // Compact constructor syntax.� Ellipse.circle(num diameter) { // A named constructor.� minorAxis = majorAxis = diameter;� }�}abstract class Shape { // Declare a class that can't be instantiated.� num get area;� num rotation = 0;� num outlineWidth = 1;� String color = 'black';�}

19 of 40

Futures:

Functions that do potentially expensive work return Futures. To specify what happens when the function's work is done, invoke then() on the Future. To handle errors, invokecatchError().

20 of 40

import 'dart:io';import 'dart:async';void printDailyNewsDigest() {� File file = new File("dailyNewsDigest.txt");� file.readAsString() // readAsString() returns a Future.� .then((content) => print(content)) // Handle successful completion.� .catchError((error) => // Handle failure.� print("Sorry, no news today. Here's why:\n$error"));�}

21 of 40

For more fun testing Dart visit

22 of 40

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

23 of 40

Dart Library

24 of 40

Object Construction With Factories

25 of 40

More Complex Example

Class person {}

Class Customer extends person {

Buy() {print(“bought”);)

}

main () {

person p = new Customer();

p.buy();

}

  • Code actually compiles and runs
  • Tere are no type errors - only Static Warnings
  • Types are considered annotations
  • Type annotations never change the semantics of a program

26 of 40

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: Chek 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 of 40

Coding differences between JavaScript and Dart

28 of 40

Code embedding

JavaScript

<script src=‘myscript.js'></script>

Google Dart

•<script type='application/dart' src='myscript.dart'></script>

•<script type='text/javascript'> if (navigator.webkitStartDart) { navigator.webkitStartDart(); } </script>

29 of 40

Entry point

JavaScript

Not required

Google Dart

• REQUIRED

–main() { }

30 of 40

Printing to the console

JavaScript

console.log(‘Hello BCB');

Google Dart

• print(‘Hello BCB');

31 of 40

Code modularity

JavaScript

No native implementation

Google Dart

• Defining library

–#library(‘BCB');

–class BCB11 { hello() => ‘Hello BCB 11'; }

•Using library

–#import(‘BCB ');

–var msg = new BCB11();

32 of 40

Variables

JavaScript

•var – mostly used for all types.

•Undeclared variable :

“undefined”

•No “final” variable support

Google Dart

•Strongly supports types variables: var, String, int, double, boolean, etc.

•Undeclared variable : “null”

•Supports “final”

33 of 40

Collections

JavaScript

No native JavaScript equivalent for unique item collection.

Google Dart

•Set for unique item collection.

34 of 40

Function

JavaScript

•function fun(a, b, c) { return c; };

–fun(1)

Result= undefined

–fun(1, 2, 3)

Result= 3

Google Dart

•fun(a, b, c) => c;

–fn(1);

Result=ERROR:NoSuchMethodException

–fn(1, 2, 3);

Result= 3

•Optional parameters

–fn(a, [b, c]) => c;

–fn('a');

Result= null

35 of 40

Function

JavaScript

•Does not have native support for named parameters

Google Dart

•myFun(x, [y, z])

{ print("Hello BCB ${x} ${y} ${z}" ); }

–myFun(11);

–myFun(11, 12);

36 of 40

For Each Loop

JavaScript

•Not available

Google Dart

•data.forEach((key, value){ print('${key}, ${value}'); });

37 of 40

Constructors

JavaScript

•function BCB(x) {

this.x = x;

};

Google Dart

•class BCB {

var x;

BCB(x) {

this.x = x;

} }

•In short

class BCB {

var x;

BCB(this.x); }

38 of 40

Classes

JavaScript

•function BCB(){

this.name=null;

};

BCB.prototype.greet=function(){

return ‘Hello, ‘ + this.name;

}

Google Dart

•class BCB {

var name;

greet() => 'Hello, $name';

}

39 of 40

Advance for loop

JavaScript

•Not available

Google Dart

•For( var x in list)

{

print(x);

}

40 of 40

+GDG Algiers

+GDG Women Algiers

oussama.zeddam@gmail.com

twitter.com/ZeddamOussama