views:

567

answers:

2

I have a project that is mixed Java/Scala, it is Java GUI code that makes use of a Scala library. Is there a way to write Scala code such that it will emit Java enums on compile time? The approaches I tried so far (sealed case classes, extend Enumeration) seem to generate normal classes which makes working with them from Java much hairier than straight up enums.

A: 

Scala only supports Java 1.4 features at this point.

Java Enums were introduced in Java 1.5.

drudru
Er, apart from generics
oxbow_lakes
Generics are implemented with type erasure, after compilation there are no generics anymore. But Scala supports annotations and those are retained when compiling to bytecode and that is most definitely a Java 1.5 feature.
Boris Terzic
Generics in java is implemented via type erasure as well
oxbow_lakes
What Boris meant is that Scala only support JVM 1.4 features. Language features that do no exist on the bytecode are irrelevant. Except, of course, interface-wise. :-)
Daniel
@Daniel, My understanding is that JVM bytecode hasn't changed since 1.0. So the only differences between 1.4 and 1.5 are language features.
Jay Conrod
The class file format was extended to accommodate at least enums and annotations, but it's possible they managed it in a backwards compatible way. JSR 202 should have the details ( http://www.jcp.org/en/jsr/detail?id=202 )
Boris Terzic
+3  A: 

Why can't you write your enum class in Java? Mixed-source (i.e. Java + Scala) projects are perfectly feasible...

oxbow_lakes
Indeed, that was also my conclusion, I will accept this answer. Maven support for mixed projects is still very flaky though.
Boris Terzic
I continue to write my enums in Java for exactly this reason.
Seth Tisue