Sunday, November 30, 2008

Inconsistencies when moving from Java to Scala

Writing the Scala serialization benchmark I did a Java to Scala calls. It is very simple, just as using a yet another Java library. You only need to add the Scala library jar into your classpath and you're ready to go.
Alas there where to small but nasty quirks

  • Scala's List is not a Java Collection list. Actually I could not create a Scala list since its abstract. Scala is using lists heavily and its list is very powerful. Still it sucks that you can't easily create a Scala list in Java and set it in a Scala object. Obviously there are easy ways around this problem.
  • The second is that Scala's enum is not a Java enum. It has many implications when trying to use a library written in Scala.

3 comments:

Itay Maman December 1, 2008 at 5:39 AM  

"Scala's List is not a Java Collection list.".

I guess you could blame immutability. In fact, I really wish Java had an immutable (you know, Lisp-like) list.

James Iry December 2, 2008 at 6:09 AM  

Creating a Scala list from Java isn't obvious, but certainly doable.

import scala.List;
import scala.List$;

public class Test {
public static void main(String[] args) {
List<String> ducks = list("huey", "duey", "louie");
}

public static <A> List<A> list(A... args) {
return List$.MODULE$.fromArray(args);
}
}

Eishay Smith December 2, 2008 at 9:33 AM  

@Itay, I agree. You can make a list immutable of course (Collections.unmodifiableList), but that would be missing the point :-)

It would be nice to have a Scala/Java utils library for java code that needs to use Scala list for language interoperability or just because its nice. List$.MODULE$.fromArray looks too much like hacking or using unsupported implementation.

Creative Commons License This work by Eishay Smith is licensed under a Creative Commons Attribution 3.0 Unported License.