2012-08-25 43 views

回答

48

由於沒有訂購,Map和Set沒有預先計劃(+:)或追加(:+)的概念。要指定您使用哪一個(追加或預先設定),請添加:

scala> Seq(1,2,3):+4 
res0: Seq[Int] = List(1, 2, 3, 4) 

scala> 1+:Seq(2,3,4) 
res1: Seq[Int] = List(1, 2, 3, 4) 

不要通過參數的順序混亂,在Scala中,如果方法結尾:it get's applied in reverse order(不a.method(B),但弘(一))

+0

謝謝,很好的答案。這很有意義。我應該自己想出來:) – thesamet

19

僅供參考,接受答案根本不是原因。這是原因。

% scala27 
Welcome to Scala version 2.7.7.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_06). 

scala> Set(1, 2, 3) + " is the answer" 
res0: java.lang.String = Set(1, 2, 3) is the answer 

scala> List(1, 2, 3) + " is the answer" 
warning: there were deprecation warnings; re-run with -deprecation for details 
res1: List[Any] = List(1, 2, 3, is the answer) 

永遠不要低估any2stringadd之類的卷鬚的長度。

+5

更清楚的是,這是由於List是協變的,但Set是不變的,在我看來,這是比any2stringadd更大的問題。 –

相關問題