Google Dart 介紹 Ticore Shih 2013/07/04
Google Dart 介紹
Ticore Shih 2013/07/04
Dart 是什麼?
• 一個全新的語言 • 針對結構化 Web 開發 • 直譯式執行 • 具有獨立的 VM • 高效能 • 沒有意外的 OOP • 多個 Isolate,單一執行緒 • 具有一定的相容性
為什麼需要 Dart ?
• 開發複雜且高效的 Web Application
充滿意外的 Javascript ? • parseint radix parseInt('fuck'); // NaN parseInt('fuck', 16); // 15
• why am i a number "Why am I a " + typeof + ""; // "Why am I a number" • infinity madness parseFloat( 'Infinity' ) // returns Infinity Number( 'Infinity' ) // returns Infinity parseInt( 'Infinity' ) // returns NaN
http://wtfjs.com/
Dart 全面改善
結構、語法、語意 核心庫、效能
測試、除錯、工具
Dart 背景
• Power by Google • Lars Bak - V8, JVMs • Gilad Bracha - Java JVM Spec. • 80+ Engineers • Syntax
– Java, C#, C++, ActionScript, JavaScript
Dart 跨平台
用同一種語言開發多種平台 – Web Server
– Web Application
– Mobile Application
– Command-line Application
Dart 效能
• DartVM 2x of V8
• Dart2JS ~= Plain JS
http://www.dartlang.org/performance http://github.com/xxgreg/deltablue
DeltaBlue
Dart 相容性
• 語法特色能完整編譯為 JS
• 支援各種瀏覽器最後兩版
• 利用既有 JS 庫 - JS Interop
• 支援 SourceMap 除錯
和諧 Harmony
• 從另一個角度看 Web 開發
• 長期可能取代 Android Java
• Dart 亦實作 ECMA 6 部分特色
Dart 開發工具
• Dart Editor – Dartium - Chromium w/ Dart VM – Dart SDK
• dart • dart2js • analyzer • dartdoc • pub
http://www.dartlang.org/docs/editor/
Hello Dart! (Command-line App)
main() => print("Hello Dart!"); main () { print("Hello Dart!"); }
Hello Dart! (Web App)
<!DOCTYPE html> <html> <body> <script type="application/dart"> main() { print("Hello Dart!"); } </script> </body> </html>
Dart in HTML 限制
• 一頁只能有一個 Dart Script Tag
• 整頁載入後才執行 main 進入點
• 不能使用 Inline Listener
• 不能動態注入 Script Tag
http://www.dartlang.org/articles/embedding-in-html/
Dart Web UI
• ToDoMVC
– Dart Editor Built-in Example
• Dart Widgets
– http://kevmoo.github.io/widget.dart/
Dart Web Server
• HTTP Time Server
– Dart Editor Built-in Example
• WebSocket Chat Server & Client
– http://www.dartlang.org/docs/dart-up-and-running/contents/ch05.html
Dart 語言特色 • 只有 true 為真
• null vs. undefined
• 簡易物件相等性
• 所有東西皆為物件
• 所有物件皆有 hashCode
Dart 字串
• 代換字串 "Hello $name!"; "Hello ${ name }!";
• 原始字串 r'raw string. \x00\a\n';
• 多行字串 '''Multi-line string.''';
• 鄰接字串 "String 1" 'String 2';
Dart 語彙空間
• 真實語彙空間
• for loop 語彙空間
• No hoisting
Dart 型別
• 選擇性型別宣告 • 型別檢查
– 開發期、執行期、不檢查
• runtimeType
• 支援泛型
Dart 函式 • 一級公民
• Closure
• 具名可選參數
• 循序可選參數
• 定義函式變數型別
Dart 類別宣告
• 類別即介面
• 抽象類別
• Mixin
• 泛型
Dart 類別建構
• 工廠模式
• 具名建構子
• 常數建構子
Dart 類別成員
• 私有成員
• getter / setter
• 模擬函式呼叫
• noSuchMethod
Dart 運算子
• 多載運算子
< , > , <= , >= , == , - , + , / , ~/ , * , % , | , ^ , & , << , >> , []= , [] , ~
• 階層運算子
..
Dart 註解 • 單行註解
// 一般註解 /// 文件註解
• 多行註解
/* 一般多行註解 */ /** 文件多行註解 */
• 巢狀註解
/* /* 巢狀註解 */ */
http://www.dartlang.org/articles/doc-comment-guidelines/
Dart 結構化管理 • allow top-level
variable, function, getter/setter, class
• declare library part files
• import library
import as, hide, show export
• library's visibility
Dart 非同步特色
• mixing sync, async call
• mixing parallel, sequential async call
• async try-catch-finally
• pass value or error object
Dart Async Future
new Future ( ... ) .then ( onValue : ... , onError : ... ) .catchError ( ... , { bool test : ... } ) .whenComplete ( ... ); Future.wait( <Future> [ ... ] );
Dart Async Error Flow
new Future( ... )
.then ( onValue : ... , onError : ... )
.catchError ( ... )
.whenComplete ( ... );
Dart 更多特色 • Lazy Load Library
http://blog.sethladd.com/2013/04/lazy-load-libraries-in-dart.html http://api.dartlang.org/docs/bleeding_edge/dart_async/DeferredLibrary.html
• Reflection (Mirror)
http://www.dartlang.org/articles/reflection-with-mirrors/
• Metadata (Annotation) http://futureperfect.info/2013/06/20/a-case-for-metadata.html
• Enum
http://news.dartlang.org/2013/04/enum-proposal-for-dart.html
• SIMD http://www.dartlang.org/slides/2013/02/Bringing-SIMD-to-the-Web-via-Dart.pdf
Dart Built-in API
http://api.dartlang.org/docs/releases/latest/
Dart Style Guide
http://www.dartlang.org/articles/style-guide/
http://www.dartlang.org/ http://try.dartlang.org/ http://blog.dartwatch.com/