Scala: Default Value for a Map of Tuples -


look @ following map:

scala> val v = map("id" -> ("_id", "$oid")).withdefault(identity) v: scala.collection.immutable.map[string,java.io.serializable] = map(id -> (_id,$oid)) 

the compiler generates map[string,java.io.serializable] , value of id can retrieved this:

scala> v("id") res37: java.io.serializable = (_id,$oid) 

now, if try access element not exist this...

scala> v("idx") res45: java.io.serializable = idx 

... expected key itself... how tuple key , empty string this?

scala> v("idx") resxx: java.io.serializable = (idx,"") 

i need tuple, regardless of whether or not element exists.

thanks.

instead of .withdefault(identity) can use

val v = map("id" -> ("_id", "$oid")).withdefault(x => (x, "")) 

withdefault takes parameter function create default value when needed.

this change return type useless serializable more useful (string, string).


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -