2

我之前曾尝试将这个问题分解为更小、更简单的问题herehere,但我意识到这些问题的答案虽然在技术上是正确的,但并不能帮助我理解这个特殊情况。

我正在使用一个库Circumflex ORM,它允许您按如下方式定义模式:

class User extends Record[Int, User] {
  val name = "name".TEXT
  val age = "age".INTEGER
  def relation = User
}
object User extends User with Table[Int, User]

这是因为 Record 范围内的隐式视图:

abstract class Record[PK, R <: Record[PK, R]] extends Equals { this: R =>
  implicit def view(x: String) = new DefinitionHelper(x, this)
  ...
}

class DefinitionHelper[R <: Record[_, R]](name: String, record: R) {
  def TEXT = ...
  def INTEGER = ...
  ...
}

我正在尝试在 TEXT 等旁边引入一种名为 BYTEA 的新扩展方法。所以我知道我需要自己的隐式助手类:

class DefinitionHelper[R <: Record[_, R]](name: String, record: R) {
 def BYTEA = new BytesField[R](name, record)
}

现在,每当我定义新记录时,我都需要一个隐式范围,但我不想每次都编写导入语句:

class User extends Record[Int, User] {
 import Implicits._
 ...
}

除了记录定义之外,我不想将此隐式引入任何其他范围。

import Implicits._
class User extends Record[Int, User] { ... }

所以一个想法是继承 Record(或引入 mixin),然后通过扩展 MyRecord 而不是 Record(或总是在 MyMixin 中混合)来定义我的模式记录。

class User extends MyRecord[Int, User] { ... }

我第一次尝试:

abstract class MyRecord[PK, R <: MyRecord[PK, R]]
extends Record[PK, R] {
  implicit def str2ddlHelper2(str: String) =
    new DefinitionHelper(str, this)
}

这会产生:

illegal inheritance;  self-type MyRecord[PK,R] does not conform to ru.circumflex.orm.Record[PK,R]'s selftype R

所以我尝试了:

abstract class MyRecord[PK, R <: MyRecord[PK, R]]
extends Record[PK, MyRecord[PK, R]] {
 implicit def str2ddlHelper2(str: String) =
   new DefinitionHelper(str, this)
}

但是在定义记录时我遇到了这两个问题:

class User extends MyRecord[Int, User] {
 val id = "id".INTEGER
 val value = "value".BYTEA // works
 val value2 = new DefinitionHelper("", this) // does not work?!
 ...
 def relation = User // another error here
}
object User extends User with Table[Int, User]

错误是:

inferred type arguments [User] do not
conform to class DefinitionHelper's type parameter bounds [R <:
ru.circumflex.orm.Record[_, R]]

type mismatch;  found   : User.type (with
underlying type object User)  required:
ru.circumflex.orm.Relation[Int,MyRecord[Int,User]]
Note: User <:
MyRecord[Int,User] (and
User.type <:
ru.circumflex.orm.Table[Int,User]), but
trait Relation is invariant in type R. You may wish to define R as +R
instead. (SLS 4.5)

经过更多的摆弄,我发现一些有用的东西让我自己感到惊讶:

abstract class MyRecord[PK, R <: MyRecord[PK, R]]
extends Record[PK, R] { this: R =>
  implicit def str2ddlHelper2(str: String) =
    new DefinitionHelper(str, this)
}

我很想知道这里发生了什么,也许还有一些例子可以帮助我更好地理解事情,这样我就不会觉得我总是“摆弄直到它起作用”。

为问题标题道歉 - 不确定它是否有意义。

4

1 回答 1

4

您的第一个错误比较简单,并且可以通过您的最终解决方案轻松解决。R=>声明中的 self 类型

Record[PK, R <: Record[PK, R]] extends Equals { this: R =>

强制每个后代Record确保它也将成为 R(它不会使其成为 R,后代将不得不做一些事情才能成为 R)。class X extends Record[PK, R]在实践中,这意味着 inR必须是 的祖先X(并且也有R <: Record[PK, R],它应该是X大多数时候,但正如我们将在最后看到的那样,情况可能并非如此)。

此约束在 MyRecord 中消失,因此您的第一个错误。您的最终解决方案再次说明了约束,这就是它的结束。


你的第二个版本更复杂。我将从第二个错误开始。

首先,来自 Circumflex API 的一些元素在上面没有说明。

  • Record有一个摘要def relation: Relation[PK, R]
  • Table[K,R]延伸Relation[K,R]

您将类User中的关系定义为对象User,它是 a Table[Int, User],因此是 a Relation[Int, User]

但是,您的课程User是 a MyRecord[Int, User],但这意味着它是 a Record[Int, MyRecord[Int, User]],而不是 a Record[Int, User]。的RRecord这里重要的)是MyRecord[Int, User],不是User。关系因此必须是Relation[Int, MyRecord[Int, User]]

ARelation[Int, User]不是 a Relation[Int, MyRecord[Int, User]],即使User是 a MyRecord[Int, User]。一般来说,如果B是一个AC[B]不是一个C[A],除非类C通过声明C[+X]而不是声明C[X]。(因此有关Relation保持不变(无 +)的消息,并建议 a+R使其在 中是协变R)。

我真的不太确定 DefinitionHelper 中的错误。它似乎与 RMyRecord[Int, User] 再次存在有关。如果您明确将其声明为通用参数R,则执行

new DefinitionHelper[MyRecord[Int, User]]("", this) 

它应该可以工作(我在一个非常接近您的代码的示例中做到了,但实际上并未使用抑扬符)。为什么编译器不推断,我不知道。无论如何,你User不是一个Record[Int, User]Record[Int, MyRecord[Int, User]]必然会引起问题的事实。实际的解决方案要简单得多。

于 2011-07-27T20:39:00.197 回答