1

I would like to use a val in a Scala object as a compile-time constant in Java (specifically in a switch). My code looks like this:

class Scala {} // in case it matters, there is a class of the same name
object Scala {
    val foo = "foo"
}

class Java {
    void switchIt(String on) {
        switch (on) {
            case Scala$.MODULE$.foo: // doesn't compile because foo isn't a field
            case Scala$.MODULE$.foo(): // doesn't compile because foo() isn't a constant
        }
    }
}

I am aware of the following questions but none solves my problem:

Is there a way to do this?

Reinstate Monica
  • 2,420
  • 14
  • 23

0 Answers0