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:
- Scala Reflection : why getMethods can return the val members? - explains that a
val
gets compiled into a method - Use Scala constants in Java - the answer suggests calling the generated method
- Scala: Compile time constants - suggests using
final val
to create compile-time constants; however, it is not clear on whether Java sees the result as a compile-time constant and doesn't appear to work in my case
Is there a way to do this?