无法弄清楚Scala Type继承,协方差,方法和列表问题

时间:2012-10-01 00:43:59

标签: scala inheritance covariance

我收到了这封编辑信息:

  

推断类型参数[sdo.core.domain.Field [_>: 2 with java.util.UUID<:java.lang.Comparable [>: 1 with java.util.UUID<:java.lang.Object] with java.io.Serializable]]不符合method ::' s类型参数bounds [B>:sdo.core.domain.Field [ >:包含org.scala_tools.time.Imports.DateTime&lt ;: java.lang.Comparable [_>:java.lang.String with org.joda.time.ReadableInstant<:java.lang的字符串.Object] with java.io.Serializable]]   [error]覆盖def fieldList = this.id :: this.create :: this.name :: this.description :: Nil

我想要的是Field[_]的列表,或与Field[_]的任何协变列表。我该怎么做?

以下是带有问题的代码:

class Work( initialId :EntityUuidIdField, 
    initialName :NameField, 
    initialDescription :TextField) extends Entity{

    val id = initialId
    val name = initialName
    val description :Field[String]= initialDescription
    val create = new DateTimeField()
    val begun = new DateTimeField()
    val inProgress = new DateTimeField()
    val done = new DateTimeField()
    val subjectiveWellBeing = new SubjectiveWellBeingField()
    val size = new WorkSizeField()

    override def fieldList = this.id :: this.create ::  this.name :: this.description       :: Nil

    }

以及类型的定义:

class DateTimeField extends Field[DateTime] {

class EntityUuidIdField( val id :UUID) extends EntityIdField[UUID]( id) {

class EntityIdField[T]( id :T) extends Field[T] {

class NameField extends Field[String] {

class Field[T] extends Signal[T] {

1 个答案:

答案 0 :(得分:1)

看起来它不喜欢这三种类型的交集;我还没有尝试-Yinfer-debug。

但List(x,y,z)没问题。

scala> trait X extends Comparable[X] with Serializable
defined trait X

scala> trait Y extends Comparable[Y] with Serializable
defined trait Y

scala> case class Foo[+A](a: A)
defined class Foo

scala> val x: Foo[X] = null
x: Foo[X] = null

scala>  val y: Foo[Y] = null
y: Foo[Y] = null

scala> val s : Foo[String] = null
s: Foo[String] = null

scala> s :: Nil
res0: List[Foo[String]] = List(null)

scala> y :: s :: Nil
res1: List[Foo[Comparable[_ >: String with Y <: java.io.Serializable] with java.io.Serializable]] = List(null, null)

scala> x :: y :: s :: Nil
<console>:15: error: inferred type arguments [Foo[Comparable[_ >: _2 with X <: java.io.Serializable] with java.io.Serializable]] do not conform to method ::'s type parameter bounds [B >: Foo[Comparable[_ >: String with Y <: java.io.Serializable] with java.io.Serializable]]
              x :: y :: s :: Nil

scala> val u: Foo[java.util.UUID] = null
u: Foo[java.util.UUID] = null

scala> x :: y :: u :: Nil
<console>:15: error: inferred type arguments [Foo[Comparable[_ >: _4 with X <: java.io.Serializable] with java.io.Serializable]] do not conform to method ::'s type parameter bounds [B >: Foo[Comparable[_ >: java.util.UUID with Y <: java.io.Serializable] with java.io.Serializable]]
              x :: y :: u :: Nil
                ^

scala> val a: Foo[AnyRef] = null
a: Foo[AnyRef] = null

scala> x :: y :: a :: Nil
res5: List[Foo[AnyRef]] = List(null, null, null)

scala> trait Z extends Comparable[Z] with Serializable
defined trait Z

scala> val z: Foo[Z] = null
z: Foo[Z] = null

scala> x :: y :: z :: Nil
<console>:16: error: inferred type arguments [Foo[Serializable with Comparable[_ >: _6 with X <: Serializable]]] do not conform to method ::'s type parameter bounds [B >: Foo[Serializable with Comparable[_ >: Z with Y <: Serializable]]]
              x :: y :: z :: Nil
                ^

scala> List(x,y,z,s,u)
res7: List[Foo[Comparable[_ >: java.util.UUID with String with Z with Y with X <: java.io.Serializable] with java.io.Serializable]] = List(null, null, null, null, null)