- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Scala/Java interop
Sun, 2008-12-21, 03:54
Hi all,
I'm trialling Scala at the moment for a future personal project, but
also with a view towards a possible use at work - however, if this is
going to happen, Java code would have to be able to make use of any
work done in Scala, and so I'd like to demonstrate Java code
seamlessly using Scala.
I'm aware that chapter 29 of the Programming in Scala book covers
this, but at this early stage I'm not able to purchase the book, so I
was wondering if anyone had some basic hints on writing Scala that
Java can use.
So far I've noted two things, the first is probably an IDE problem -
when calling a Scala class from Java, my IDE (IDEA 8) does not
recognise the Scala class' constructor - that is, for class Foo (a:
Int), the IDE only recognises a constructor with nil arguments - but
the code compiles correctly, has anyone had this problem in other
IDEs?
The second thing I've noted is that for a class
class B(var a: Int)
B.a is a public field when being called within Scala, but attempting
to compile Java that accesses this field causes an error, with the
message that field a of class B is non-public. I'm not sure why it
happens, but I'm guessing that using the @BeanProperty annotation to
create getters/setters for the Java code to use would fix it, as all
my public methods are accessible.
Are there any other gotchas I should be aware of?
Regards,
Liam Clarke
Feel free to use a better disassembler. :-)
~/sctemp> cat Foo.scala
class Foo(bar : Int) {
var blurb = "42"
}
~/sctemp> scalac Foo.scala
~/sctemp> javap -c -private Foo
Compiled from "Foo.scala"
public class Foo extends java.lang.Object implements scala.ScalaObject{
private java.lang.String blurb;
public Foo(int);
Code:
0: aload_0
1: ldc #10; //String 42
3: putfield #14; //Field blurb:Ljava/lang/String;
6: aload_0
7: invokespecial #19; //Method java/lang/Object."<init>":()V
10: return
public void blurb_$eq(java.lang.String);
Code:
0: aload_0
1: aload_1
2: putfield #14; //Field blurb:Ljava/lang/String;
5: return
public java.lang.String blurb();
Code:
0: aload_0
1: getfield #14; //Field blurb:Ljava/lang/String;
4: areturn
public int $tag() throws java.rmi.RemoteException;
Code:
0: aload_0
1: invokestatic #35; //Method scala/ScalaObject$class.$tag:(Lscala/ScalaObject;)I
4: ireturn
}
On Sat, Dec 20, 2008 at 6:54 PM, Liam Clarke-Hutchinson <liam@steelsky.co.nz> wrote: