Giter Club home page Giter Club logo

Comments (2)

lloydmeta avatar lloydmeta commented on July 17, 2024

Hey there,

Thanks for asking this question ! I'm not super experienced w/ calling Scala stuff from Java, but gave it a try and the following seems to work:

I think it's just a matter of syntax? Accessing nested singleton Scala objects from Java should be something like Outer$Inner$.MODULE$, assuming object Outer { object Inner }

// Given
import enumeratum._

sealed trait MyEnum extends EnumEntry

case object MyEnum extends Enum[MyEnum] {

  val values = findValues

  case object First  extends MyEnum
  case object Second extends MyEnum
  case object Third  extends MyEnum

}

The folllowing Java seems to compile fine:

class ExampleUse {
    public static void go() {
        System.out.println(MyEnum$First$.MODULE$);
    }
}

Scala: 2.12.4
Enumeratum: 1.5.12

from enumeratum.

martin-g avatar martin-g commented on July 17, 2024

In my case the difference is that MyEnum is inside another object.
Finally I was able to resolve it by introducing "factory methods":

object Transfer {

  sealed trait Type extends EnumEntry

  // the methods are for Java interop
  object Type extends Enum[Type] {
    val values: immutable.IndexedSeq[Type] = findValues

    case object DEPOSIT extends Type
    def deposit(): DEPOSIT.type = DEPOSIT

    case object WITHDRAWAL extends Type
    def withdrawal(): WITHDRAWAL.type = WITHDRAWAL
  }
}

and in the Java code I call them with Transfer.Type$.MODULE$.withdrawal()

Hope this helps someone else!

Thank you!

from enumeratum.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.