Why do case class companion objects extend FunctionN?

Posted by retronym on Stack Overflow See other posts from Stack Overflow or by retronym
Published on 2010-06-15T21:58:18Z Indexed on 2010/06/15 22:02 UTC
Read the original article Hit count: 276

Filed under:
|
|
|

When you create a case class, the compiler creates a corresponding companion object with a few of the case class goodies: an apply factory method matching the primary constructor, equals, hashCode, and copy.

Somewhat oddly, this generated object extends FunctionN.

scala> case class A(a: Int)                                 
defined class A

scala> A: (Int => A)
res0: (Int) => A = <function1>

This is only the case if:

  • There is no manually defined companion object
  • There is exactly one parameter list
  • There are no type arguments
  • The case class isn't abstract.

Seems like this was added about two years ago. The latest incarnation is here.

Does anyone use this, or know why it was added? It increases the size of the generated bytecode a little with static forwarder methods, and shows up in the #toString() method of the companion objects:

scala> case class A()
defined class A

scala> A.toString
res12: java.lang.String = <function0>

© Stack Overflow or respective owner

Related posts about class

Related posts about scala