2014-07-16 52 views

回答

2

這裏是2元組實現:

以恆定的構造函數:

class Tuple{ 
    final _1, _2; 
    foo()=> _1 + _2; 
    const Tuple(this._1,this._2); 
} 

void main() { 
    var a = const Tuple(10,20); 
    var b = const Tuple(10,20); 
    print(a); 
    print(b); 
    print(a.foo()); 
} 

有了一個新的構造:

class Tuple{ 
    final _1, _2; 
    foo()=> _1 + _2; 
    Tuple(this._1,this._2); 
} 
void main() { 
    var a = new Tuple(10,20); 
    var b = new Tuple(10,20); 
    print(a); 
    print(b); 
    print(a.foo()); 
} 

dart2js outputs comparison

那是怎樣new Tuple看起來輸出:

main: function() { 
    P.print(new S.Tuple(10, 20)); 
    P.print(new S.Tuple(10, 20)); 
    P.print(30); 
} 

const Tuple僅創建一次C.Tuple_10_20 = new S.Tuple(10, 20);像這樣使用:

main: function() { 
    P.print(C.Tuple_10_20); 
    P.print(C.Tuple_10_20); 
    P.print(C.Tuple_10_20._1 + C.Tuple_10_20._2); 
} 

注意,在new Tuple的情況下,函數調用已替換爲它的返回值直譯,但它沒有發生的const Tuple輸出。

+0

請在答案中加入差異的相關部分;您的鏈接將設置爲在一年內過期。 – Ryan

+3

這就是我們(dart2js-team)應該解決的問題。 Const對象應該始終至少和非const對象一樣好。 –

相關問題