Scala的映射和集定義了一個+
運算符,該運算符返回數據結構的副本並附加一個元素。 Seq
的等價操作符表示爲:+
。爲什麼(複製)附加到Scala中的Seq被定義爲:+而不僅僅是+和Set和Map一樣?
這是否有任何不一致的原因?
Scala的映射和集定義了一個+
運算符,該運算符返回數據結構的副本並附加一個元素。 Seq
的等價操作符表示爲:+
。爲什麼(複製)附加到Scala中的Seq被定義爲:+而不僅僅是+和Set和Map一樣?
這是否有任何不一致的原因?
由於沒有訂購,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),但弘(一))
僅供參考,接受答案根本不是原因。這是原因。
% 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之類的卷鬚的長度。
更清楚的是,這是由於List是協變的,但Set是不變的,在我看來,這是比any2stringadd更大的問題。 –
謝謝,很好的答案。這很有意義。我應該自己想出來:) – thesamet