7

I can't find how copy is implemented for case class in scala.

Can I check it somehow?

I though Intellij could point me to implementation, but it doesn't want to jump and I have no idea why :/

kpbochenek
  • 469
  • 2
  • 21
  • 7
    It is generated by scalac, it would look something like this http://stackoverflow.com/a/6637597/5123895 To actually see it I think you would need to compile and decompile or enable some option in scalac. – Łukasz Feb 25 '16 at 12:03

1 Answers1

4

You can inspect the scala case class output using scalac -print ClassName.scala, as the copy is actually a compiler generated method.

Here's a given example:

case class Test(s: String, i: Int)

This is the output after filtering out noise for copy:

case class Test extends Object with Product with Serializable {
    private[this] val s: String = _;
    def s(): String = Test.this.s;

    private[this] val i: Int = _;
    def i(): Int = Test.this.i;

    def copy(s: String, i: Int): common.Test = new common.Test(s, i);
    def copy$default$1(): String = Test.this.s();
    def copy$default$2(): Int = Test.this.i();
}
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321