我试图使用type字段,但似乎我做错了什么。 它似乎与经典类一起工作,但由于某种原因,案例类的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提供 。
I was trying to use type field, but it seems I did something wrong. It seems to be working with classic classes, but for some reason type of case class's apply doesn't match.
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 }Error
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 foundCode is also available at Ideone.
最满意答案
我怀疑你有一个关于案例类错误的细节:
案例类没有免费应用功能!
它们带有一个伴随对象,该对象具有工厂apply函数来创建案例类的新实例。 那是:
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函数是您想要的吗?
I suspect you got one detail about case classes wrong:
Case classes do not come with free apply functions!
They come with a companion object that has a factory apply function to create new instances of your case class. That is:
case class Foo(bar: Int)is similar to
class Foo(val bar: Int) object Foo { def apply(bar: Int): Foo = new Foo(bar) }So, in your code
case class TestCaseClassStore(data: Map[IdRef[Int], Object]) extends IdStore { override type Entity = Object override type Ref = IdRef[Int] override type Self = TestCaseClassStore }is missing the apply function required in IdStore.
Considering this, are you sure that the apply function defined in IdStore is what you want?
更多推荐
发布评论