|| 考虑以下内容(经过Scala 2.8.1和2.9.0测试):
trait Animal
class Dog extends Animal

case class AnimalsList[A <: Animal](list:List[A] = List())
case class AnimalsMap[A <: Animal](map:Map[String,A] = Map())

val dogList = AnimalsList[Dog]()  // Compiles
val dogMap = AnimalsMap[Dog]()    // Does not compile
最后一行失败:
error: type mismatch;
 found   : scala.collection.immutable.Map[Nothing,Nothing]
 required: Map[String,Main.Dog]
Note: Nothing <: String, but trait Map is invariant in type A.
You may wish to investigate a wildcard type such as `_ <: String`. (SLS 3.2.10)
Error occurred in an application involving default arguments.
    val dogMap = AnimalsMap[Dog]()    // Does not compile
                       ^
one error found
将其更改为“ 2”可以修复该问题,但不再利用默认参数值。 考虑到List对应项按预期工作,为什么将默认值推断为Map [Nothing,Nothing]?有没有一种方法可以创建一个AnimalsMap实例,该实例使用
map
arg的默认值? 编辑:我已经接受了我提出的更为紧迫的第二个问题的答案,但是我仍然想知道为什么在这两种情况下,以不同的方式推断出
Map()
的键类型:
case class AnimalsMap1(map:Map[String,Animal] = Map())
val dogs1 = AnimalsMap1() // Compiles

case class AnimalsMap2[A <: Animal](map:Map[String,A] = Map())
val dogs2 = AnimalsMap2[Dog]() // Does not compile
编辑2:似乎类型界限是不相关的-案例类的任何参数类型都会引起问题:
case class Map3[A](map:Map[String,A] = Map())
val dogs3 = Map3[Dog]() // Does not compile