Google Dart language
What is Dart Language ?
Dart : A Structure web programming language
It’s a :
*New Open source project
*New programming language
*New programming tools
The team Behind Dart
1/ Lars Back
>18 software patents
>V8 JavaScript VM in Google chrome
>HotSpot Java VM
>HotSpot Java VM
The team Behind Dart
2/ Gilad bracha
> Computational theologist and later Distinguished Engineer at sun
> Java language specification
>Java virtual machine specification
Why Dart
>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.
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.
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.
The Code : Syntax
Dart aims to be unsurprising, but it does add a few language features to keep your code succinct yet understandable.
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.�
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}');�}
Hi,Dart
Basic web app with the obligatory "Hello, world" example.
HI.DART
library hi;
import 'dart:html';
main() {
query('#status').text = 'Hi, Dart';
}
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>
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.
// Without types.�recalculate(origin, offset, estimate) {� // ...�}
// With types (and an optional parameter).num recalculate(Point origin,� num offset,� {bool estimate:false}) {� // ...�}
Classes
Dart is object oriented, with full support for classes. Each class has a single superclass, but classes can implement any number of interfaces.
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';�}
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().
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"));�}
For more fun testing Dart visit
Namespaces
#Library(“http”);
#import(“http.dart”);
#import(“http.dart”,”http”);
Dart Library
Object Construction With Factories
More Complex Example
Class person {}
Class Customer extends person {
Buy() {print(“bought”);)
}
main () {
person p = new Customer();
p.buy();
}
On Types
> i.e.java int:values all integers from-2147483648 to 2147483647
> Operations on int: +-* etc
> i.e. multiply a String by an int
> i.e. call a method that does not exist on an object
Coding differences between JavaScript and Dart
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>
Entry point
JavaScript
Not required
Google Dart
• REQUIRED
–main() { }
Printing to the console
JavaScript
console.log(‘Hello BCB');
Google Dart
• print(‘Hello BCB');
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();
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”
Collections
JavaScript
No native JavaScript equivalent for unique item collection.
Google Dart
•Set for unique item collection.
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
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);
For Each Loop
JavaScript
•Not available
Google Dart
•data.forEach((key, value){ print('${key}, ${value}'); });
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); }
Classes
JavaScript
•function BCB(){
this.name=null;
};
BCB.prototype.greet=function(){
return ‘Hello, ‘ + this.name;
}
Google Dart
•class BCB {
var name;
greet() => 'Hello, $name';
}
Advance for loop
JavaScript
•Not available
Google Dart
•For( var x in list)
{
print(x);
}
Thank You