Why is this not compiling?
The given error class SomeElement needs to be abstract, since method eval in trait Element of type [T <: Typed]=> scala.util.Try[T] is not defined
I do not understand why the eval method defined in SomeElement does not satisfy type restrictions.
As I understand it, eval should return something enclosed in Try , which are subclasses of Typed . Try is covariant in its type parameter. The implementation of eval in SomeElement returns a NumberLike and NumberLike subclasses of Typed . So what went wrong?
import scala.util.Try trait Element { def eval[T <: Typed]: Try[T] } trait Typed case class NumberLike(n: Long) extends Typed case class SomeElement(n: Long) extends Element { def eval = Try { NumberLike(n) } } object app extends Application { println (SomeElement(5).eval) }
Trying to add an explicit type parameter to eval in SomeElement will not help:
case class SomeElement(n: Long) extends Element { def eval[NumberLike] = Try { NumberLike(n) } }
Changing the definition of SomeElement to the above gives:
found : <empty>.NumberLike required: NumberLike(in method eval) NumberLike(n)
EDIT I would really like to know why this does not compile. Workarounds for this problem are helpful, but I really want to know what is going on here.
types scala
Squidly
source share