Why Scala REPL shows tuple type for Map expression?

一曲冷凌霜 提交于 2019-12-11 05:34:31

问题


Scala REPL gives the same type for both expressions - (tuple? -- strange!). Yet ("a" ->1) which is a Map I can add to map and ("a", 1)can not. Why Scala REPL shows tuple type type for Map expression?

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)

回答1:


Actually, the reason for this is that Predef: http://www.scala-lang.org/api/current/index.html#scala.Predef$ (which is always in scope in Scala) contains an implicit conversion from Any to ArrowAssoc (the method implicit def any2ArrowAssoc[A](x: A): ArrowAssoc[A])

ArrowAssoc contains the method -> which converts it to a tuple.

So basically you are doing any2ArrowAssoc("a").->(1) which returns ("a",1).

From repl:

any2ArrowAssoc("a").->(1)
res1: (java.lang.String, Int) = (a,1)

Furthermore, you can work on immutable hashmaps like this:

val x = HashMap[Int,String](1 -> "One")
x: scala.collection.immutable.HashMap[Int,String] = Map((1,One))
val y = x ++ HashMap[Int,String](2 -> "Two")
y: scala.collection.immutable.Map[Int,String] = Map((1,One), (2,Two))
val z = x + (3 -> "Three")
z: scala.collection.immutable.HashMap[Int,String] = Map((1,One), (3,Three))



回答2:


Scala thinks a + (b,c) means you are trying to call the + method with two arguments, which is a real possibility since maps do have a multi-argument addition method so you can do things like

m + (("a" -> 1), ("b" -> 2))

the solution is simple: just add an extra set of parentheses so it's clear that (b,c) is in fact a tuple being passed as a single argument.

m + (("a", 1))


来源:https://stackoverflow.com/questions/15602983/why-scala-repl-shows-tuple-type-for-map-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!