推断的类型参数
问题描述:
为什么在编译此代码段时出现错误?推断的类型参数
trait ID[R <: Record[R] with KeyedRecord[Long]] {
this: R =>
val idField = new LongField(this)
}
错误:
inferred type arguments [ID[R] with R] do not conform to class LongField's
type parameter bounds [OwnerType <: net.liftweb.record.Record[OwnerType]]
我该如何解决这个问题?
LongField定义:
class LongField[OwnerType <: Record[OwnerType]](rec: OwnerType)
extends Field[Long, OwnerType] with MandatoryTypedField[Long] with LongTypedField {
答
转换
val idField = new LongField(this)
到
val idField = new LongField[R](this)
如果不指定类型R
,那么LongField无法检查的典型e是否为Record[OwnerType]
的共变体。明确提及它应该解决目的。
PS:我不知道其他类的声明再次确认,但下面的声明的工作原理:
case class Record[R]
class KeyedRecord[R] extends Record[R]
class LongField[OwnerType <: Record[OwnerType]](rec: OwnerType)
trait ID[R <: Record[R] with KeyedRecord[Long]] {
this: R =>
val idField = new LongField[R](this)
}