Oplægget blev holdt ved et seminar i InfinIT-interessegruppen Højniveausprog til indlejrede systemer den 12. marts 2014. Læs mere om interessegruppen her: http://infinit.dk/dk/interessegrupper/hoejniveau_sprog_til_indlejrede_systemer/hoejniveau_sprog_til_indlejrede_systemer.htm
2. #dartlang
Object oriented language
Optional typing
Single-threaded event model
Isolates for concurrency
Compiles to JavaScript
Native VM
✔ Improved productivity
✔ Increased performance
16. #dartlang
Clean semantics and behavior
● Only true is truthy
● There is no undefined, only null
● No type coercion with ==, +
No invisible magic
Examples:
17. #dartlang
Missing getter?
"hello".missing // ??
Class 'String' has no instance getter 'missing'.
NoSuchMethodError : method not found: 'missing'
Receiver: "hello"
Arguments: []
Logical
19. #dartlang
Variable scope?
var foo = 'top-level';
main() {
if (true) { var foo = 'inside'; }
print(foo); // ?? What will this print?
}
top-level
Logical
No
hoisting
20. #dartlang
Scope of this?
class AwesomeButton {
AwesomeButton(button) {
button.onClick.listen((Event e) => this.atomicDinosaurRock());
}
atomicDinosaurRock() {
/* ... */
}
}
Lexical
this
27. #dartlang
Mixins
abstract class Persistable {
save() { ... }
load() { ... }
toJson();
}
class Hug extends Greeting with Persistable {
Map toJson() => {'strength':10};
}
main() {
var embrace = new Hug();
embrace.save();
}
Extend object &
no constructors?
You can be a
mixin!
Apply the mixin.
Use methods
from mixin.
30. #dartlang
Futures
var future = new File(‘foo.txt’).readAsString();
future.then((content) {
// Read the file into `content`;
}).catchError((error) {
// Failed to read the file
});
computePi();
31. #dartlang
import 'dart:io';
import 'dart:async';
import 'dart:convert';
Future<String> maybeGetPub() {
var file = new File('foo.txt');
return file.exists().then((exists) {
return file.readAsString();
}).then((content) {
if (content.contains('bard')) {
return new HttpClient().getUrl(Uri.parse('http://pub.dartlang.org'));
} else {
return new HttpClient().getUrl(Uri.parse('http://golang.org'));
}
}).then((request) {
return request.close(); // could send data first
}).then((response) {
var completer = new Completer();
var content = "";
response.transform(UTF8.decoder)
.listen((data) => content = "$content$data",
onDone: () => completer.complete(content));
return completer.future;
}).catchError((error) {
return error; // Failed to read the file
});
}
Future<String> getServedString(String content) {
if (content.contains('dartlang.org')) return new Future.value("Served");
return Process.run('cat', ['foo.txt'])
.then((result) => "No beer: ${result.stdout}");
}
void main() {
maybeGetPub().then(getServedString).then(print);
}
41. #dartlang
addConstraintsConsumingTo$2: function(v, coll) {
var determining, t1, t2, t3, i, t4, c;
determining = v.get$determinedBy();
t1 = v.constraints;
t2 = getInterceptor$as(t1);
t3 = getInterceptor$a(coll);
i = 0;
while (true) {
t4 = t2.get$length(t1);
if (typeof t4 !== "number") throw new IllegalArgumentError(t4);
if (!(i < t4)) break;
c = t2.$index(t1, i);
if ((c == null ? determining != null : c !== determining) && c.isSatisfied$0() === true) {
t3.add$1(coll, c);
}
++i;
}
}
Simply not good
enough!
DeltaBlue: Unoptimized
42. #dartlang
Global optimizations
● Dart is structured and allows whole program analysis
● Understanding your program’s structure enables:
○ Code navigation and great refactoring tools
○ Global compile-time optimizations
43. #dartlang
DeltaBlue: Fully optimized
addConstraintsConsumingTo$2: function(v, coll) {
var determining, t1, i, c;
determining = v.determinedBy;
for (t1 = v.constraints, i = 0; i < t1.length; ++i) {
c = t1[i];
if (c !== determining && c.isSatisfied$0()) {
coll.push(c);
}
}
}
262 characters,
same semantics
44. #dartlang
Dart VM
● Up to 2x speed of JavaScript
○ Well defined classes
○ No hoisting, no magic
● 10x faster potential startup time compared to
JavaScript
○ Snapshots cache
○ SDK snapshot
● Runs serverside
○ Complete IO library (HTTP, Process, File System)