Scala Case Class disassembled

If we create a very simple Case Class in Scala:

$ echo 'case class TestCaseClass(a:Int)' > TestCaseClass.scala

and after compiling it we attempt to disassemble with:

javap -private TestCaseClass

we get:

public class TestCaseClass extends java.lang.Object implements scala.ScalaObject,scala.Product,scala.Serializable{
    private final int a;
    public static final scala.Function1 andThen(scala.Function1);
    public static final scala.Function1 compose(scala.Function1);
    public scala.collection.Iterator productIterator();
    public scala.collection.Iterator productElements();
    public int a();
    public TestCaseClass copy(int);
    public int copy$default$1();
    public int hashCode();
    public java.lang.String toString();
    public boolean equals(java.lang.Object);
    public java.lang.String productPrefix();
    public int productArity();
    public java.lang.Object productElement(int);
    public boolean canEqual(java.lang.Object);
    private final boolean gd1$1(int);
    public TestCaseClass(int);
}

Notice the wealth of helper classes created for us from the Scala compiler like the hashCode(), toString() and equals(..) methods that we routinely create in every new Java class we construct. Also our state variable is encapsulated as a constant and surprisingly we even get a factory method therefore reducing the need for calling new per each creation of instance objects.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s