問題は についてAnyRef.clone()
ではなく、同様のセマンティックを持つケースについてです。
それ自体のコピーを作成する可能性のあるクラスのインターフェイスを定義したいと思います。
trait Cloneable {
def clone() : this.type
}
class Test(val store : Int) extends Cloneable {
def clone() = new Test(store)
}
はクラス タイプと を拡張するクラスで異なるため、パス依存this.type
は機能しません。次に、子孫は clone メソッドをオーバーライドして、独自の型に一致させる必要があります。this.type
Test
Test
Cloneable トレイトの型要件をどのように定義すればよいですか?
私は scala コレクションをのぞき見しましたが、ここでヒントを見つけました:TestLike
型制限を処理する trait と、Test
対応する trait を具体化するクラスを定義します。
不必要な不器用さはできれば避けたい
提案された自己反復パターンを試す:
trait Cloneable[A <: Cloneable[A]] {
def clone() : A
}
class Store[A <: Cloneable[A]](val store : Int) extends Cloneable[A] {
override def clone() : A = new Store[A](store)
}
エラーで失敗しました:
Cloneable.scala:6: error: type mismatch;
found : Store[A]
required: A
override def clone() : A = new Store[A](store)
反復テンプレートの別の問題: 時期尚早のファイナライズ
class Store(val store : Int) extends Cloneable[Store] {
override def clone() = new Store(store)
}
class SubStore(store : Int, val stash : Double) extends Store(store)
val ss1 = new SubStore(1, 0.5)
val ss2 = ss1.clone()
assert(ss2.isInstanceOf[SubStore])
の問題は、クラスに存在しないメソッドをSubStore
無視する型システムにありますが、を介した子孫です。しかし、型パラメーターとそのすべての子孫を持つファイナライズインターフェイスには、適切なメソッド制限がありませんclone()
SubStore
SubStore
Cloneable
Store
Store
Cloneable
Store
clone()