In Scala code that I am writing, I have a Map[String, AnyRef]. When I try to initialize the Map using the following, Scala complains that it is expecting a Map[String, AnyRef] but the value is a Map[String, Any]:
val myMap: Map[String, AnyRef] =
Map("foo" -> true, "bar" -> false)
I know that I can use the following instead:
val myMap: Map[String, AnyRef] =
Map("foo" -> true.asInstanceOf[AnyRef], "bar" -> false.asInstanceOf[AnyRef])
I declared the following in scope:
implicit def booleanToAnyRef(value: Boolean): AnyRef = value.asInstanceOf[AnyRef]
but the compiler still complains.
Shouldn’t the compiler use the implicit method to convert the primitive boolean values into AnyRef values? Is there any way, short of (the ugly) x.asInstanceOf[AnyRef] to have these converted?
For the record, as the other answers suggest, the latest compiler will say:
The latest compiler will always be a friend who gives better advice than the friend you used to hang with and get into trouble together with.