Scala REPL 为这两个表达式提供了相同的类型——(元组?——奇怪!)。然而("a" ->1)
,这是一张我可以添加到地图但不能添加的地图("a", 1)
。为什么 Scala REPL 显示 Map 表达式的元组类型类型?
scala> :t ("a" -> 1)
(String, Int)
scala> :t ("a",1)
(String, Int)
scala> val m = Map.empty[String, Int]
m: scala.collection.immutable.Map[String,Int] = Map()
scala> m + ("a",1)
<console>:9: error: type mismatch;
found : String("a")
required: (String, ?)
m + ("a",1)
^
scala> m + ("a" ->1)
res19: scala.collection.immutable.Map[String,Int] = Map(a -> 1)