Why Scala REPL shows tuple type for Map expression? -


scala repl gives same type both expressions - (tuple? -- strange!). yet ("a" ->1) map can add map , ("a", 1)can not. why scala repl shows tuple type type 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) 

actually, reason predef: http://www.scala-lang.org/api/current/index.html#scala.predef$ (which in scope in scala) contains implicit conversion arrowassoc (the method implicit def any2arrowassoc[a](x: a): arrowassoc[a])

arrowassoc contains method -> converts tuple.

so doing any2arrowassoc("a").->(1) returns ("a",1).

from repl:

any2arrowassoc("a").->(1) res1: (java.lang.string, int) = (a,1) 

furthermore, can work on immutable hashmaps 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)) 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -