What is the easiest way to deeply clone (copy) a mutable Scala object?

后端 未结 2 834
伪装坚强ぢ
伪装坚强ぢ 2021-02-07 12:11

What is the easiest way to deeply clone (copy) a mutable Scala object?

2条回答
  •  不知归路
    2021-02-07 12:32

    A Java-specific solution (which should work great in Scala too), is the Cloner library. It's fast, easy, deeply clones objects based on fields (using reflection), and is smart enough not to clone known immutable objects (like String, Integer, etc.). Finally, you can register custom immutable objects so it won't clone them either.

    I highly recommend it.

提交回复
热议问题