複製鏈接
請複製以下鏈接發送給好友

DART

(計算機編程語言)

鎖定
Dart是谷歌開發的計算機編程語言,後來被Ecma (ECMA-408)認定為標準。 [1]  它被用於web服務器移動應用物聯網等領域的開發。它是寬鬆開源許可證(修改的BSD證書)下的開源軟件。 [2] 
Dart是面向對象的、類定義的、單繼承的語言。它的語法類似C語言,可以轉譯為JavaScript,支持接口(interfaces)、混入(mixins)、抽象類(abstract classes)、具體化泛型(reified generics)、可選類型(optional typing)和sound type system [3] 
軟件名稱
Dart
上線時間
2011年10月10日
開發商
谷歌
項目創建者
Lars Bak,Kasper Lund

DART產生背景

Dart亮相於2011年10月10日至12日在丹麥奧爾胡斯舉行的GOTO大會上 [4]  。該項目由Lars bak和kasper lund創建。

DART發展歷程

Ecma國際組織組建了技術委員會TC52 [5]  來開展Dart的標準化工作,並且在Dart可以編譯為標準JavaScript的情況下,它可以在任何現代瀏覽器中有效地工作。Ecma國際組織於2014年7月第107屆大會批准了Dart語言規範第一版,並於2014年12月批准了第二版 [6] 
2015年5月Dart開發者峯會上,亮相了基於Dart語言的移動應用程序開發框架Sky [7-8]  ,後更名為Flutter。
2018年2月,Dart2成為強類型語言 [9] 

DART代碼示例

hello world例子
在終端打印字符串‘Hello World!’
main() {  
    print('Hello World!');
}
int fib(int n) => (n > 2)
 ? (fib(n - 1) + fib(n - 2)) 
 : 1;
void main() {
  print('fib(20) = ${fib(20)}');
}

一個簡單的類
計算兩點距離
// 引入math庫以訪問sqrt函數
import 'dart:math' as math;
// 創建類Point.
class Point {
  // Final變量一經定義不可改變
  // 創建分別代表x、y軸的距離變量
  final num x, y;
  // 在構造方法中以語法糖快捷地設置實例變量
  Point(this.x, this.y);
  // 一個帶有初始化列表的命名構造方法
  Point.origin()
    : x = 0,
      y = 0;
  // 計算兩點距離的方法
  num distanceTo(Point other) {
    var dx = x - other.x;
    var dy = y - other.y;
    return math.sqrt(dx * dx + dy * dy);
  }
  // 重載運算符
  Point operator +(Point other) => new Point(x + other.x, y + other.y);
}

// 所有的Dart程序都以main()函數作為入口
void main() {
  // 實例化兩個點
  var p1 = new Point(10, 10);
  var p2 = new Point.origin();
  // 計算兩點距離
  var distance = p1.distanceTo(p2);
  print(distance);
}

異步併發示例
使用了Isolate
import 'dart:async';
import 'dart:isolate';

main() async {
  var receivePort = new ReceivePort();
  await Isolate.spawn(echo, receivePort.sendPort);
  // 'echo'發送的第一個message,是它的SendPort
  var sendPort = await receivePort.first;
  var msg = await sendReceive(sendPort, "foo");
  print('received $msg');
  msg = await sendReceive(sendPort, "bar");
  print('received $msg');
}
/// 新isolate的入口函數
echo(SendPort sendPort) async {
  // 實例化一個ReceivePort 以接收消息
  var port = new ReceivePort();
  // 把它的sendPort發送給宿主isolate,以便宿主可以給它發送消息
  sendPort.send(port.sendPort);
  // 監聽消息    
  await for (var msg in port) {
    var data = msg[0];
    SendPort replyTo = msg[1];
    replyTo.send(data);
    if (data == "bar") port.close();
  }
}
/// 對某個port發送消息,並接收結果
Future sendReceive(SendPort port, msg) {
  ReceivePort response = new ReceivePort();
  port.send([msg, response.sendPort]);
  return response.first;
}
參考資料