How does Scala java conversion work?

scala, scala-java-interop

Solution

`JavaConversions` object contains many implicit conversions between Scala->Java and Java->Scala collections. When you import all members of `JavaConversions`, all of those conversions are put in the current scope, and are therefore evaluated when an immediate collection type isn't available.

For example, when Scala compiler is looking for a collection of type `X` and cannot find it, it will also try to find a collection of type `Y` and an implicit conversion `Y to X` in scope.

To understand more about how the conversions are evaluated, see this answer.

Problem

If I have java.util.List and want to iterate over it user Scala syntax I import : ``` import scala.collection.JavaConversions._ ``` and the java.util.List is implicitly converted to scala.collection.mutable.Set (http://www.scala-lang.org/api/current/index.html#scala.collection.JavaConversions%24) But how is this conversion achieved ? I'm confused as this is the first time I've encountered the ability to convert an object type by just importing a package.

Original source

Related problems