Just give the compiler a little help:
case class AnimalsMap[A <: Animal](map:Map[String,A] = Map[String, A]()) ^^^^^^^^^^^
I'll leave the details of why your solution doesn't work to someone better acquainted with Scala's type inference…
Edit: See IttayD's answer for a nice explanation of this behavior.
Scala has a feature where you can define a class to be covariant / contravariant in its generic parameters.
As an example to covariance: it is natural to think that if
class Student extends Person then
List[Person]. This is because every method that accepts a
List[Person] should have no problem working with an object
List[Student]. This is not possible in Java (without making the method also generic).
Contravariance is the opposite and is a bit trickier to explain. It is required when the type is supposed to be pushed to the generic class, not read (in
List[Person] you read the elements of the list). The general example is a function. The types of the arguments to a function are put into it, so if a method expects a function
Person => String it can't be called with a function
Student => String (it will call the argument with a person, but it expects a student)
Scala also defines
Nothing to implicitly extend everything. It is the bottom type. So
List[Nothing] always "extends"
List[X] for any X.
List[Nothing] and the covariance is why you can write
val x: List[Person] = List().
Anyways, Map is invariant in its key types. The reason is that a
Map[A, B] is like a function
A => B so it can only be contravariant in
A. Another way is to think what happens if you pass a
Map[Student, String] to a method expecting
Map[Person, String], obviously it may try to put
Person objects in there which is not good, the other way is OK. On the other hand, a Map can be viewed as
Iterable[(A, B)], here it should be covariant in A. So it is invariant in its value.
The result is you cannot assign a
Map[Nothing, Nothing] to a variable of type
Map() creates a
The compiler tells you this:
scala> val dogs3 = Map3[Dog]() <console>:13: error: type mismatch; found : scala.collection.immutable.Map[Nothing,Nothing] required: Map[String,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 dogs3 = Map3[Dog]() ^
- Problem with bounded type parameterised case class and default args in Scala
- Scala type inference not working with generic case class and lambda
- Scala copy case class with generic type
- Scala wont pattern match with java.lang.String and Case Class
- Scala getting field and type of field of a case class
- Scala case class copy doesn't always work with `_` existential type
- How do I write a scala extractor for a case class with default parameters?
- scala type class with higher kinded types and variance
- Get case class field's name and type with shapeless
- How to partially apply case class with type parameter in Scala
- How to get Scala case class fields and values as (String, String) with Shapeless or Macro
- scala overrides java class method with inner class and type parameters
- Automatic type class derivation for case classes with Scala Enumeration fields
- Scala macro annotation - case class with type parameters
- Change fields of case class based on map entries with simple type check in scala macros
- Instantiating a case class with default args via reflection
- Scala problems with slick 2, when case class and object name are same
- Scala List with abstract class type and accessing to child
- Scala case class and companion object can't override apply with diff signature
- Class with covariant type parameter and private constructor in Scala
- Scala case class with argument type change at runtime
- Scala Generics with Case Class and Object Companion
- Creating a case class for an embedded MongoDB Array with Salat and Scala
- Issue with trait and case class inheritance: scala
- Scala - type mismatch problem with own type (String) and Future
- Scala 3: Deriving a typeclass for conversion between a case class and a tuple type
- How Scala allows overriding with type parameters and not with type class
- Spark Scala case class with array and map datatype
- How to define case class with a list of tuples and access the tuples in scala
- Why Scala case class copy method parameterised only with the variables defined in the case class?
More Query from same tag
- Why are Arrays invariant, but Lists covariant?
- how do I fix a bug in the scala-library.jar
- Spark textFile vs wholeTextFiles
- how can i get another form input verified in play?
- Scala - extending MainFrame(), GridBagPanel() and what about 'reactions'
- Why isn't Scala used much for Desktop Applications?
- Kyotocabinet and Scala(Java): Limit of DB files open?
- Easiest way to access ZIO test generators from Scala REPL?
- Storing Result of SQL Query in RDD
- Iterate Spark Dataframe running slow
- Is foldRight equivalent to foldLeft given a noncommutative associative operation?
- How to RegExp file with Spark?
- Mix normal method calls with messages when using Actors?
- Create Future without starting it
- Scala Slick 2 join on multiple fields?
- Initialising vals which might throw an exception
- Scala - map a value from a Map to another Map
- Playspec Mock actor for unit test
- how to properly submit a spark job?
- How to use group by for multiple columns with count?
- SBT: Ampersand (&) is not a member of sbt.io.FileFilter
- scala, IntelliJ: stackoverflow exception
- How to run jar inside SBT task?
- Scala custom unapply with generics
- How to add unmanaged jars to build.sbt generated by a g8 template?
- Cannot install new 0.11.3 sbt on Ubuntu 10.04LTS
- Parsing Nested JSON Data using SCALA
- Build issues for apache spark
- Why there is always a .collect() after a .distinct()?
- Playframework with Deadbolt2: overriding method getSubject in trait DeadboltHandler of type