I understand that Scala's JavaConversions adds implicit conversions between various Java and Scala collections. I understand how that works in Scala, with the implicit keyword.
However, in my case, I am running some Java code, and it has at the top:
import scala.collection.JavaConversions;
Now somehow, seemingly by magic, having that import at the top changes the code below when Java collections are used.
For instance, I have a method:
public static Collection<String> foo() { return ... }
and if I call that method from outside, the Collection instance I get back is ... altered, somehow. It has extra methods added. Through some mechanism I don't understand, that code — which does not involve Scala or JavaConversions at all — causes the class loader to bring in Scala classes.
How does it work?
The Java language itself does not have an implicit keyword or extension methods, but Scala does. Somehow, importing that Scala library is doing something low-level that (presumably) can't be expressed in Java but is still supported by the JVM.
- What happens when that
importis encountered? - How does Scala add methods to existing Java classes?
- What is the timing involved, here? Does something happen at
importtime, at class-load time, or something else?