我在Scala 2.12.3中,看起来像withDefaultMethod
不起作用。如果None
:
Map
> console
[info] Starting scala interpreter...
[info]
Welcome to Scala 2.12.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_141).
Type in expressions for evaluation. Or try :help.
scala> val scores = Map("Alice" -> 100, "Bob" -> 80, "Cindy" -> 99)
scores: scala.collection.immutable.Map[String,Int] = Map(Alice -> 100, Bob -> 80, Cindy -> 99)
scala> val scores1 = scores.withDefaultValue(0)
scores1: scala.collection.immutable.Map[String,Int] = Map(Alice -> 100, Bob -> 80, Cindy -> 99)
scala> val aaaScore = scores1.get("aaa")
aaaScore: Option[Int] = None
scala>
我在这里缺少什么?
答案 0 :(得分:4)