Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I "get" a Scala case object from Java?

I created a hierarchy of case objects in Scala that looks like the following:

package my.awesome.package  sealed abstract class PresetShapeType(val displayName: String)  case object AccelerationSensor extends PresetShapeType("Acceleration Sensor") case object DisplacementSensor extends PresetShapeType("Displacement Sensor") case object ForceSensor        extends PresetShapeType("Force Sensor") case object PressureSensor     extends PresetShapeType("Pressure Sensor") case object StrainSensor       extends PresetShapeType("Strain Sensor") 

I also have a piece of Java code in which I'd like to access PressureSensor, but the following does not work:

package my.awesome.package.subpackage;  import my.awesome.package.PressureSensor;  // Do some stuff, then...  DVShape newshape = DVShapeFactory.createPresetShape(PressureSensor, new Point3f(0,0,0)); 

So, how do I reference the PressureSensor case object from Java? I decompiled the byte code for both the PressureSensor and PressureSensor$ classes, which yielded the following:

Compiled from "DVShapeFactory.scala" public final class org.nees.rpi.vis.PressureSensor extends java.lang.Object{     public static final java.lang.Object productElement(int);     public static final int productArity();     public static final java.lang.String productPrefix();     public static final int $tag();     public static final java.lang.String displayName(); }  Compiled from "DVShapeFactory.scala" public final class org.nees.rpi.vis.PressureSensor$ extends org.nees.rpi.vis.PresetShapeType implements scala.ScalaObject,scala.Product,java.io.Serializable{     public static final org.nees.rpi.vis.PressureSensor$ MODULE$;     public static {};     public org.nees.rpi.vis.PressureSensor$();     public java.lang.Object readResolve();     public java.lang.Object productElement(int);     public int productArity();     public java.lang.String productPrefix();     public final java.lang.String toString();     public int $tag(); } 

But that didn't yield any great insight.

like image 558
mipadi Avatar asked Apr 01 '10 15:04

mipadi


People also ask

What is a Scala case object?

A case object is like an object , but just like a case class has more features than a regular class, a case object has more features than a regular object. Its features include: It's serializable. It has a default hashCode implementation. It has an improved toString implementation.

What is difference between Case class and case object in Scala?

A case class can take arguments, so each instance of that case class can be different based on the values of it's arguments. A case object on the other hand does not take args in the constructor, so there can only be one instance of it (a singleton, like a regular scala object is).

What is case _ in Scala?

case _ => does not check for the type, so it would match anything (similar to default in Java). case _ : ByteType matches only an instance of ByteType . It is the same like case x : ByteType , just without binding the casted matched object to a name x .


2 Answers

from Java, say:

my.awesome.package.PressureSensor$.MODULE$ 
like image 70
Seth Tisue Avatar answered Oct 16 '22 17:10

Seth Tisue


PressureSensor$.MODULE$ should give you the instance of the case object.

like image 32
Geoff Reedy Avatar answered Oct 16 '22 17:10

Geoff Reedy