Case class copy() method abstraction

前端 未结 5 863
孤独总比滥情好
孤独总比滥情好 2021-01-02 02:08

I would like to know if it is possible to abstract the copy method of case classes. Basically I have something like sealed trait Op and then something like

5条回答
  •  情深已故
    2021-01-02 02:29

    You seem to be confusing copy with clone. The goal of copy is to make an almost identical copy, but with something changed. What that something might be depends on the parameters of the case class, so it's not possible to make it a common method.

    In the case of case class X(), it doesn't make much sense to have a copy method, as there's nothing there to be changed.

    On the other hand, clone is a Java method whose goal is to produce perfect copies of an object, which seems to be what you want.

提交回复
热议问题