The Artima Developer Community
Sponsored Link

Programming in Scala Forum
Calling Scala from Java

3 replies on 1 page. Most recent reply: Dec 25, 2007 2:48 AM by Justin Forder

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Randolph Kahle

Posts: 6
Nickname: randykahle
Registered: Apr, 2006

Calling Scala from Java Posted: Dec 22, 2007 5:06 AM
Reply to this message Reply
Advertisement
I am investigating Scala as a language for integration with NetKernel. NetKernel is written in Java and its microkernel invokes a method on a Java class which implements functionality. (In NetKernel we call this an Accessor - it is similar to a Servlet in J2EE frameworks).

If I want to define a Scala class that implements a Java Interface or even inherits from a Java class (JDK 1.4 based) how can I do this?

-- Randy


Justin Forder

Posts: 9
Nickname: jufo
Registered: May, 2007

Re: Calling Scala from Java Posted: Dec 24, 2007 3:58 AM
Reply to this message Reply
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

Randolph Kahle

Posts: 6
Nickname: randykahle
Registered: Apr, 2006

Re: Calling Scala from Java Posted: Dec 24, 2007 5:10 AM
Reply to this message Reply
Justin,

Thank you!

This makes sense and I did make some more progress this morning.

I'm stuck again on an issue and I'll post that as a new question to the forum.

Kind regards,

Randy

Justin Forder

Posts: 9
Nickname: jufo
Registered: May, 2007

Re: Calling Scala from Java Posted: Dec 25, 2007 2:48 AM
Reply to this message Reply
My example relied on the fact that you can use extends with a trait if there is no superclass to be specified. More generally, with can be used with Java interfaces as well as with Scala traits:

class ScalaEcho extends AnyRef with Echo {

Flat View: This topic has 3 replies on 1 page
Topic: NetKernel and Scala Previous Topic   Next Topic Topic: Use of

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use