2017-03-06 67 views
0

這是我曾嘗試如何複製案例類對象?

sealed abstract class Tree 
case class NewNode(left: Tree,center: Tree, right: Tree) extends Tree 

我只是想複製整個對象

scala> val treeM = treeD.copy_ 
<console>:16: error: value copy_ is not a member of NewNode 
     val treeM = treeD.copy_ 
        ^

以上不工作

scala> val treeM = treeD.copy(_,_,_) 
treeM: (Tree, Tree, Tree) => NewNode = <function3> 

scala> println("Tree M == Tree D: %s" format (treeM == treeD).toString) 
<console>:18: warning: (Tree, Tree, Tree) => NewNode and NewNode are unrelated: they will most likely never compare equal 
     println("Tree M == Tree D: %s" format (treeM == treeD).toString) 

如何編寫正確的對象副本?

+0

你能解釋一下爲什麼你想這樣做嗎? –

回答

1

撥打電話copy沒有任何參數。

val treeM = treeD.copy() 
1

你可以,但沒有理由複製不可變對象。