Consider the following (tested with Scala 2.8.1 and 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
The last line fails with:
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
Changing it to val dogMap = AnimalsMap[Dog](Map())
fixes it, but no longer takes advantage of the default argument value.
Why is the default value being inferred as Map[Nothing,Nothing], given that List counterpart works as expected? Is there a way to create an AnimalsMap instance that uses the default value for the map
arg?
Edit: I've accepted an answer to my more-pressing second question, but I'd still be interested to know why the key type to Map()
is inferred differently between these two cases:
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
Edit 2: Seems that type bounds are irrelevant - any parametric type to the case class causes the problem:
case class Map3[A](map:Map[String,A] = Map())
val dogs3 = Map3[Dog]() // Does not compile