我之前曾尝试将这个问题分解为更小、更简单的问题here和here,但我意识到这些问题的答案虽然在技术上是正确的,但并不能帮助我理解这个特殊情况。
我正在使用一个库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)
}
我很想知道这里发生了什么,也许还有一些例子可以帮助我更好地理解事情,这样我就不会觉得我总是“摆弄直到它起作用”。
为问题标题道歉 - 不确定它是否有意义。