Type mismatch; found: newValue.type (with base type S): T

What I want to do:

class ListField[+T] extends Field[MutableList[T]] {

  value = Some(new MutableList[T]())

  def +=[S >: T](newValue: S): Unit = add(newValue)

  def add[S >: T](newValue: S): Unit = value.map(l => {

    if (writable_? || !initialized_?) {

In the next line, I get:

type mismatch; found: newValue.type (with base type S) required: T

      l += newValue
      validate
      makeDirty
      change0.fire(l)
  }
})

def length: Int = list.length
//etc

How should I do it? Is it possible? If I do not do the T-co-option, everything works as it is.

Field:

class Field[T] extends Signal[T] with ChangeStateTracking {

  private[this] var data: Option[T] = None

  private var writable = true

  def writable_? = writable

  override def now = data.get

  def value: Option[T] = data

  def validate: ValidationNel[FieldError, Option[T]] = value.successNel[FieldError]
//etc

NOTE. The code really works and compiles if I don't want T to be covariant (i.e. T instead of + T). This is only when I try to make T covariant to get an error.

I need an extra list + T, which may change.

+3
source share
1 answer

Well, the type lis equal MutableList[T], so you obviously can't call +=with any other type.

, , l - ListField, += MutableList, ListField.

+1

All Articles