推断的类型参数

时间:2013-05-01 07:59:27

标签: scala parameters self-type inferred-type

编译此代码段时,为什么会出现错误?

trait ID[R <: Record[R] with KeyedRecord[Long]] {

  this: R =>

  val idField = new LongField(this)
}

ERROR:

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 {

1 个答案:

答案 0 :(得分:1)

转换

val idField = new LongField(this)

val idField = new LongField[R](this)

如果您未指定类型R,则LongField无法检查该类型是否与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)
}