1

If mapAsScalaMap is an implicit function then why do we have to use it explicitly? And why is recursive conversion not happening ?

import scala.collection.JavaConversions._  
val javaObj = yamlObj.asInstanceOf[java.util.LinkedHashMap[String, java.util.LinkedHashMap[String, String]]]

val map: collection.mutable.Map[String, collection.mutable.Map[String, String]] = javaObj << type mismatch

Isn't the above supposed to work because I imported all implicit Java conversions?

If I use mapAsScalaMap(javaObj) then it works but then it doesn't convert nested LinkedHashMap into a Scala map.

1
  • Scala's javaconversions don't work for nested conversions but an explicit conversion can be done for them using JavaConverters as shown at stackoverflow.com/questions/13981983/… not in the direction you want but can be adapted to convert nested java map to scala. Commented Jul 6, 2015 at 21:53

1 Answer 1

0

Implicit conversions don't happen "inside" the parameters of a parametrized type, and don't happen recursively.

You can convert the values using something like this:

val map: collection.Map[String, collection.mutable.Map[String, String]] = javaObj.mapValues(x => x: collection.mutable.Map[String, String])

(note that map does not offer a mutable interface - it's a view on the original map but it can't be modified because scala doesn't know how to run the transformation in reverse).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.