Merging of custom and compiler generated companion objects for a case class. What are the merging rules?

StackOverflow https://stackoverflow.com/questions/22760814

Question

I just tried out this code below and it worked as expected. It prints 1.

Now, my problem is that I don't understand what is going on under the hood.

How can a case class have two companion objects (One generated by the compiler and one written by me) ? Probably it cannot. So they must be merged somehow under the hood. I just don't understand how are they merged ? Are there any special merging rules I should be aware of ?

Is it so that, if the set of definitions defined in both companion objects are disjoint then the set of definitions in the resulting case class is simply the union of two disjoint sets ? I would think this is how they are merged, but I am not sure. Can someone please confirm whether this merging rule is the one that is implemented in the Scala compiler? Or is there something extra to it ?

More specifically, what are the rules by which the compiler generated companion object and my companion object are merged ? Are these rules specified somewhere ?

I have not really seen this topic discussed in the few Scala books I have - perhaps too superficially - read.

object A{
  implicit def A2Int(a:A)=a.i1
}
case class A(i1:Int,i2:Int)

object Run extends App{
  val a=A(1,2)
  val i:Int=a
  println(i)
}
Was it helpful?

Solution

I'm not aware of where the algorithm for merging automatic and explicit companion objects is described or documented (other than the compiler source) but by compiling your code and then examining the generated companion object (using javap), we can see what the differences are (this is with scala 2.10.4).

Here's the companion object generated for the case class (without your additional companion object):

  Compiled from "zip.sc"
  public final class A$ extends scala.runtime.AbstractFunction2<Object, Object, A> implements scala.Serializable {
    public static final A$ MODULE$;
    public static {};
    public A apply(int, int);
    public scala.Option<scala.Tuple2<java.lang.Object, java.lang.Object>> unapply(A);

    public java.lang.Object apply(java.lang.Object, java.lang.Object);
    public final java.lang.String toString();
  }

After adding your companion object, here's what is generated:

  Compiled from "zip.sc"
  public final class A$ implements scala.Serializable {
    public static final A$ MODULE$;
    public static {};
    public A apply(int, int);
    public scala.Option<scala.Tuple2<java.lang.Object, java.lang.Object>> unapply(A);
    public int A2Int(A);
  }

The differences in the generated companion object caused by the explicit companion object definition appear to be:

  1. it no longer extends AbstractFunction2
  2. it no longer has the factory method (apply) related to bullet 1
  3. it no longer overrides the toString method (I suppose you are expected to supply one, if needed)
  4. your A2Int method is added

If the case class is changed to an ordinary class (along with minimal changes required to get it to compile), the result is the following:

  Compiled from "zip.sc"
  public final class A$ {
    public static final A$ MODULE$;
    public static {};
    public A apply(int, int);
    public int A2Int(A);
  }

So it seems that if you declare your own companion object, at least in this simple example, the effect is that your new method is added to the companion object, and some of it's implementation and functionality are lost as well. It would be interesting to see what would happen if we tried to override some of the remaining auto-generated stuff, but there's not much left, so that in general is unlikely to cause conflict.

Some of the benefits of case classes are unrelated to the generated code, such as making the class variables public without having to explicitly add the 'val' keyword. Here's the modified source code for all 3 decompiled examples above.

version 1 (no explicit companion object):

case class A(i1:Int,i2:Int)

version 2 is your original version.

version 3 (no case-class):

object A {
  implicit def A2Int(a:A)=a.i1
  def apply(a:Int,b:Int):A = new A(a,b)
}
class A(val i1:Int,val i2:Int)

object Run extends App{
  import A._
  val a=A(1,2)
  val i:Int=a
}

In version 3, we need to add val to class A parameters (otherwise they're private), and we have to either add the factory method to our companion object, or use the 'new' keyword when creating an instance of A(1,2).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top