case class 的复制方法似乎丢失了
Copy method of case class seems to be missing
我无法理解为什么 copy
方法没有 created/picked 以防万一 class TestCaseClassStore
.
abstract class IdStore {
self =>
type Entity
type Ref <: IdRef[_]
type Self <: IdStore {type Entity = self.Entity; type Ref = self.Ref}
def copy(data: Map[Ref, Entity]): Self
def data: Map[Ref, Entity]
def merge(other: Self): Self = copy(data ++ other.data)
}
trait IdRef[T] {
def id: T
}
case class IntIdRef(id:Int) extends IdRef[Int]
class TestStore(val data: Map[IntIdRef, Object]) extends IdStore {
override type Entity = Object
override type Ref = IntIdRef
override type Self = TestStore
override def copy(newData: Map[Ref, Entity]): Self = new TestStore(newData)
override def toString() = "TestStore(" + data + ")"
}
case class TestCaseClassStore(data: Map[IntIdRef, Object]) extends IdStore {
override type Entity = Object
override type Ref = IntIdRef
override type Self = TestCaseClassStore
}
object Main extends App {
val data = Map(IntIdRef(1) -> "target 1", IntIdRef(2) -> "target 2");
val classic = new TestStore(data)
println(classic.copy(classic.data))
val caseClass = new TestCaseClassStore(data)
println(caseClass.copy(caseClass.data))
}
这以未定义的方法错误结束:
Main.scala:30: error: class TestCaseClassStore needs to be abstract, since method copy in class IdStore of type (data: Map[TestCaseClassStore.this.Ref,TestCaseClassStore.this.Entity])TestCaseClassStore.this.Self is not defined
case class TestCaseClassStore(data: Map[IntIdRef, Object]) extends IdStore {
^
one error found
如果我在 case class 中添加以下行,它可以工作,但它毫无用处,因为我想使用 case class 的主要原因是自动生成的复制方法.
override def copy(newData: Map[Ref, Entity]): Self = TestCaseClassStore(newData)
为什么 TestCaseClassStore
上没有 copy
方法?也许是编译器或语言的限制?或者我是否错误地实现了超级 class(但是 TestStore
class 正在工作)?
是否有一些简洁的方式来编写更多IdStore
的实现(最好不要重复copy
方法,甚至以某种方式抽象Entity
和 Ref
构造函数中的类型,因此它们在类型定义中只被提及一次)?
如果复制方法已经存在,编译器不会生成。
你可以看到,通过在超级class中为复制方法添加一个实现,它不会被覆盖。我怀疑这是故意的。
我无法理解为什么 copy
方法没有 created/picked 以防万一 class TestCaseClassStore
.
abstract class IdStore {
self =>
type Entity
type Ref <: IdRef[_]
type Self <: IdStore {type Entity = self.Entity; type Ref = self.Ref}
def copy(data: Map[Ref, Entity]): Self
def data: Map[Ref, Entity]
def merge(other: Self): Self = copy(data ++ other.data)
}
trait IdRef[T] {
def id: T
}
case class IntIdRef(id:Int) extends IdRef[Int]
class TestStore(val data: Map[IntIdRef, Object]) extends IdStore {
override type Entity = Object
override type Ref = IntIdRef
override type Self = TestStore
override def copy(newData: Map[Ref, Entity]): Self = new TestStore(newData)
override def toString() = "TestStore(" + data + ")"
}
case class TestCaseClassStore(data: Map[IntIdRef, Object]) extends IdStore {
override type Entity = Object
override type Ref = IntIdRef
override type Self = TestCaseClassStore
}
object Main extends App {
val data = Map(IntIdRef(1) -> "target 1", IntIdRef(2) -> "target 2");
val classic = new TestStore(data)
println(classic.copy(classic.data))
val caseClass = new TestCaseClassStore(data)
println(caseClass.copy(caseClass.data))
}
这以未定义的方法错误结束:
Main.scala:30: error: class TestCaseClassStore needs to be abstract, since method copy in class IdStore of type (data: Map[TestCaseClassStore.this.Ref,TestCaseClassStore.this.Entity])TestCaseClassStore.this.Self is not defined
case class TestCaseClassStore(data: Map[IntIdRef, Object]) extends IdStore {
^
one error found
如果我在 case class 中添加以下行,它可以工作,但它毫无用处,因为我想使用 case class 的主要原因是自动生成的复制方法.
override def copy(newData: Map[Ref, Entity]): Self = TestCaseClassStore(newData)
为什么
TestCaseClassStore
上没有copy
方法?也许是编译器或语言的限制?或者我是否错误地实现了超级 class(但是TestStore
class 正在工作)?是否有一些简洁的方式来编写更多
IdStore
的实现(最好不要重复copy
方法,甚至以某种方式抽象Entity
和Ref
构造函数中的类型,因此它们在类型定义中只被提及一次)?
如果复制方法已经存在,编译器不会生成。
你可以看到,通过在超级class中为复制方法添加一个实现,它不会被覆盖。我怀疑这是故意的。