Java enum companion object (or equivalent)

With scala enums I often like to do one of the following two things to avoid having to import the enum:

package object foo {
  type MyScalaEnum =
  val  MyScalaEnum =


def foo(f: MyScalaEnum.type => MyScalaEnum) = doSomething(f(MyScalaEnum)) // so can use eg .foo(_.MyValue); incidentally, does this technique have a name?

Unfortunately neither the val assignment nor the .type call seem to be work for java enums

Any suggestions? Thanks!

I once struggled with similar problem (aliasing Java class for its static methods) and I don’t think it’s possible. Java classes don’t have an actual object so there’s no runtime value for JavaClass.type

1 Like

Thanks, that’s what I suspected too.

I toyed with javap:

$ cd ~/scala
$ cat Day.scala
object Day extends Enumeration {  

$ scalac Day.scala

$ javap Day.class 
Compiled from "Day.scala"
public final class Day {
  public static scala.Enumeration$Value SATURDAY();
  public static scala.Enumeration$Value FRIDAY();
  public static scala.Enumeration$Value THURSDAY();
  public static scala.Enumeration$Value WEDNESDAY();
  public static scala.Enumeration$Value TUESDAY();
  public static scala.Enumeration$Value MONDAY();
  public static scala.Enumeration$Value SUNDAY();
  public static scala.Enumeration$ValueSet$ ValueSet();
  public static scala.Enumeration$ValueOrdering$ ValueOrdering();
  public static scala.Enumeration$Value withName(java.lang.String);
  public static scala.Enumeration$Value apply(int);
  public static int maxId();
  public static scala.Enumeration$ValueSet values();
  public static java.lang.String toString();

$ javap Day$.class 
Compiled from "Day.scala"
public final class Day$ extends scala.Enumeration {
  public static Day$ MODULE$;
  public static {};
  public scala.Enumeration$Value SUNDAY();
  public scala.Enumeration$Value MONDAY();
  public scala.Enumeration$Value TUESDAY();
  public scala.Enumeration$Value WEDNESDAY();
  public scala.Enumeration$Value THURSDAY();
  public scala.Enumeration$Value FRIDAY();
  public scala.Enumeration$Value SATURDAY();

$ cd ~/java
$ cat
public enum Day {

$ javac
$ javap Day.class 
Compiled from ""
public final class Day extends java.lang.Enum<Day> {
  public static final Day SUNDAY;
  public static final Day MONDAY;
  public static final Day TUESDAY;
  public static final Day WEDNESDAY;
  public static final Day THURSDAY;
  public static final Day FRIDAY;
  public static final Day SATURDAY;
  public static Day[] values();
  public static Day valueOf(java.lang.String);
  static {};

There is indeed no Day$.class equivalent for java, though it makes me wonder how Day.MONDAY works in java (I’d expect to have to do new Day().MONDAY, but my java is rusty)

ah, it’s probably because of that (java can’t have top-level static classes).

Maybe a macro annotation approach that would annotate an object and take the java enum as input to reproduce all the values in the object… That seems awfully complicated though. I tried toying macro paradise the other day but ran into a lot of version problems with it.