2013-05-01 128 views
0

爲什麼在編譯此代碼段時出現錯誤?推斷的類型參數

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 { 

回答

1

轉換

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) 
}