Calling withDefaultValue when creating HashMap

66 views
Skip to first unread message

Dinotzar Tzar

unread,
Dec 6, 2011, 5:39:23 AM12/6/11
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.

Jason Zaugg

unread,
Dec 6, 2011, 5:49:19 AM12/6/11
to Dinotzar Tzar, scala...@googlegroups.com
On Tue, Dec 6, 2011 at 11:39 AM, Dinotzar Tzar <dino...@gmail.com> wrote:
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)

Map#withDefaultValue [1] returns a proxy that wraps the original map. Calls to that proxy fall back to the default value.

In `nogo`, you created such a proxy in the constructor of your anonymous subclass of HashMap, and discarded it. (A constructor can't return an object of some other type).

Dinotzar Tzar

unread,
Dec 6, 2011, 5:55:30 AM12/6/11
to Jason Zaugg, scala...@googlegroups.com
Alright. Makes sense. Thanks!

D.

Reply all
Reply to author
Forward
0 new messages