【发布时间】:2011-07-27 19:19:34
【问题描述】:
我之前尝试将这个问题分解为更小、更简单的问题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)
}
现在,每当我定义新记录时,我都需要一个隐式范围,但是 不想每次都写import语句:
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)
}
我很想了解这里刚刚发生的事情,也许还有一些例子可以帮助我更好地理解事情,这样我就不会觉得我总是“摆弄直到它起作用” 。”
为问题标题道歉 - 不确定它是否有意义。
【问题讨论】:
标签: scala circumflex-orm