Dinotzar Tzar
unread,Dec 6, 2011, 5:39:23 AM12/6/11Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Sign in to report message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to scala...@googlegroups.com
Dear rocket scientists,
most probably a silly question, but can anyone explain the difference between the following two ways of calling withDefaultValue on a (mutable) HashMap? I would expect them to work the same way.
Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_26).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import collection.mutable.HashMap
import collection.mutable.HashMap
scala> val ok = new HashMap[String,Int]().withDefaultValue(47)
ok: scala.collection.mutable.Map[String,Int] = Map()
scala> val nogo = new HashMap[String,Int]() { withDefaultValue(47) }
nogo: scala.collection.mutable.HashMap[String,Int] = Map()
scala> ok("how many road must a man walk down?")
res0: Int = 47
scala> nogo("this will hurt")
java.util.NoSuchElementException: key not found: this will hurt
at scala.collection.MapLike$class.default(MapLike.scala:224)
at scala.collection.mutable.HashMap.default(HashMap.scala:43)
at scala.collection.MapLike$class.apply(MapLike.scala:135)
at scala.collection.mutable.HashMap.apply(HashMap.scala:43)
at .<init>(<console>:10)
at .<clinit>(<console>)
at .<init>(<console>:11)
at .<clinit>(<console>)
at $print(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:704)
at scala.tools.nsc.interpreter.IMain$Request$$anonfun$14.apply(IMain.scala:920)
at scala.tools.nsc.interpreter.Line$$anonfun$1.apply$mcV$sp(Line.scala:43)
at scala.tools.nsc.io.package$$anon$2.run(package.scala:25)
at java.lang.Thread.run(Thread.java:662)
scala>
D.