scala case class copy implementation

旧时模样 提交于 2021-02-18 11:40:08

问题


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 :/


回答1:


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();
}


来源:https://stackoverflow.com/questions/35626038/scala-case-class-copy-implementation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!