Case class 的 apply 方法类型
Case class's apply method type
我试图使用 type
字段,但似乎我做错了什么。它似乎与 classic classes 一起工作,但由于某种原因,案例类型 class 的 apply
不匹配。
object Common {
type EntityId = Long
}
import Common._
abstract class IdStore {
self =>
type Entity
type Ref <: IdRef[_]
type Self <: IdStore {type Entity = self.Entity; type Ref = self.Ref}
def apply(data: Map[Ref, Entity]): Self
def data: Map[Ref, Entity]
def merge(other: Self): Self = apply(data ++ other.data)
}
trait IdRef[T] {
def id: T
}
trait EntityIdRef extends IdRef[EntityId] {}
class TestStore(val data: Map[IdRef[Int], Object]) extends IdStore {
override type Entity = Object
override type Ref = IdRef[Int]
override type Self = TestStore
override def apply(data: Map[Ref, Entity]): Self = new TestStore(data)
}
case class TestCaseClassStore(data: Map[IdRef[Int], Object]) extends IdStore {
override type Entity = Object
override type Ref = IdRef[Int]
override type Self = TestCaseClassStore
}
错误
Main.scala:34: error: class TestCaseClassStore needs to be abstract, since method apply in class IdStore of type (data: Map[TestCaseClassStore.this.Ref,TestCaseClassStore.this.Entity])TestCaseClassStore.this.Self is not defined
case class TestCaseClassStore(data: Map[IdRef[Int], Object]) extends IdStore {
^
one error found
代码也可在 Ideone 获得。
错误消息说的是,我认为这是正在发生的事情,是您覆盖了所有类型的 IdStore,但没有为 "apply" 方法这样做。
您在 "TestStore" class 中做对了,但在 "TestCaseClassStore" 中做错了。
那只是因为 case class
而 没有 apply()
方法。它是 伴随对象 得到一个 apply
方法,但不是 class.
不过,为了给您一个解决方案,我们需要更多地了解使用 IdStore
及其子 classes。假设 IdStore
是完美的,您可能一开始就不希望 TestCaseClassStore
成为 case class
。
我怀疑你有一个关于案例的细节class是错误的:
案例classes不自带免费申请功能!
它们附带一个伴随对象,该对象具有工厂 apply
函数来创建您的案例 class 的新实例。即:
case class Foo(bar: Int)
类似于
class Foo(val bar: Int)
object Foo {
def apply(bar: Int): Foo = new Foo(bar)
}
因此,在您的代码中
case class TestCaseClassStore(data: Map[IdRef[Int], Object]) extends IdStore {
override type Entity = Object
override type Ref = IdRef[Int]
override type Self = TestCaseClassStore
}
缺少 IdStore
中所需的 apply
功能。
考虑到这一点,你确定IdStore
中定义的apply
函数是你想要的吗?
我试图使用 type
字段,但似乎我做错了什么。它似乎与 classic classes 一起工作,但由于某种原因,案例类型 class 的 apply
不匹配。
object Common {
type EntityId = Long
}
import Common._
abstract class IdStore {
self =>
type Entity
type Ref <: IdRef[_]
type Self <: IdStore {type Entity = self.Entity; type Ref = self.Ref}
def apply(data: Map[Ref, Entity]): Self
def data: Map[Ref, Entity]
def merge(other: Self): Self = apply(data ++ other.data)
}
trait IdRef[T] {
def id: T
}
trait EntityIdRef extends IdRef[EntityId] {}
class TestStore(val data: Map[IdRef[Int], Object]) extends IdStore {
override type Entity = Object
override type Ref = IdRef[Int]
override type Self = TestStore
override def apply(data: Map[Ref, Entity]): Self = new TestStore(data)
}
case class TestCaseClassStore(data: Map[IdRef[Int], Object]) extends IdStore {
override type Entity = Object
override type Ref = IdRef[Int]
override type Self = TestCaseClassStore
}
错误
Main.scala:34: error: class TestCaseClassStore needs to be abstract, since method apply in class IdStore of type (data: Map[TestCaseClassStore.this.Ref,TestCaseClassStore.this.Entity])TestCaseClassStore.this.Self is not defined
case class TestCaseClassStore(data: Map[IdRef[Int], Object]) extends IdStore {
^
one error found
代码也可在 Ideone 获得。
错误消息说的是,我认为这是正在发生的事情,是您覆盖了所有类型的 IdStore,但没有为 "apply" 方法这样做。
您在 "TestStore" class 中做对了,但在 "TestCaseClassStore" 中做错了。
那只是因为 case class
而 没有 apply()
方法。它是 伴随对象 得到一个 apply
方法,但不是 class.
不过,为了给您一个解决方案,我们需要更多地了解使用 IdStore
及其子 classes。假设 IdStore
是完美的,您可能一开始就不希望 TestCaseClassStore
成为 case class
。
我怀疑你有一个关于案例的细节class是错误的:
案例classes不自带免费申请功能!
它们附带一个伴随对象,该对象具有工厂 apply
函数来创建您的案例 class 的新实例。即:
case class Foo(bar: Int)
类似于
class Foo(val bar: Int)
object Foo {
def apply(bar: Int): Foo = new Foo(bar)
}
因此,在您的代码中
case class TestCaseClassStore(data: Map[IdRef[Int], Object]) extends IdStore {
override type Entity = Object
override type Ref = IdRef[Int]
override type Self = TestCaseClassStore
}
缺少 IdStore
中所需的 apply
功能。
考虑到这一点,你确定IdStore
中定义的apply
函数是你想要的吗?