|
|
Re: Calling Scala from Java
|
Posted: Dec 24, 2007 3:58 AM
|
|
Randy, since nobody knowledgeable has answered I'll tell you the little I know.
The extends keyword in Scala can be followed by the name of a Java interface or a Java class. You can import Java interfaces and classes using normal Scala import statements.
Example:
Echo.java
package echo;
public interface Echo {
String echo(String s);
}
ScalaEcho.scala
package test
import echo.Echo
class ScalaEcho extends Echo { def echo(s: String): String = s + " from Scala"
}
Java code can call this straightforwardly:
Main.java
import echo.Echo; import test.ScalaEcho;
public class Main { public static void main(String[] args) { Echo e = new ScalaEcho(); System.out.println(e.echo("Hello")); } }
Specialising a Java class is similarly easy.
There are details of how Scala compiles to Java classes, and the implications for interoperability between Scala and Java code, in Chapter 25 of the book (Combining Scala and Java). Given that this chapter gives guidance on the use of a Scala compiler directive -target:jvm-1.5 when working with Java annotations, I have the impression (but haven't checked) that you should be OK with Java 1.4 with the default compilation behaviour.
--Justin
|
|