简体   繁体   中英

LiftWeb not Covariant Type In Cell

I am dealing with Cell and I have a problem because they are not covariant. Here is what I want to do:

import net.liftweb.util.Cell

trait A {}
class AA extends A {}

trait T{
  val cell:Cell[A]
}

class U extends T{
  val cell:Cell[AA] = //implementation
}

I have an error because AA is a descendent of A but not equal to A .

Is there a solution?

Actually, you're mistaken. Your error is "error: overriding value cell in trait T of type Cell[AA]; method cell needs to be a stable, immutable value: def cell:Cell[AA]".

Now, while I might suggest having T take a type parameter V <: A and then having the cell() function return a Cell[V], the real problem here is not even really related to generics. In T, your 'cell' is a data member. In U, 'cell' is a function. The compiler simply wants you to choose one (and the one that works and makes sense is for it to be a function in both places, so... just change that "val cell" to "def cell" and give one of those 'cell' definitions an implementation, and you're fine).

Update (now that the question has been fixed):

Alright, so, as suggested in my original answer, you'll want trait T to take a type parameter in order to solve this, like so:

trait A {}
class AA extends A {}

trait T[V <: A]{
  val cell:Cell[V]
}

class U(inCell: Cell[AA]) extends {val cell = inCell} with T[AA]
  1. From scaladoc definition trait Cell [T] extends Dependent , Cell is not covariant

  2. val can override parameterless method, but not vice versa.

  3. Either class U should be abstract or cell function defined.


If Cell is covariant, with these changes your example should work.

scala> trait Cell[+T]
defined trait Cell

scala> trait A
defined trait A

scala> class AA extends A
defined class AA

scala> trait T { def cell: Cell[A] }
defined trait T

scala> class U extends T { override def val: Cell[AA] = new Cell[AA] {} }
defined class U

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM