2
votes

I created a trait with the following signature and methods (details are omitted):

trait Cachable[A <: java.io.Serializable] {

   def push(key: String, value: A) = ???

   def pull(key: String): A = ???
}

The following code does not compile (Document class implements java.io.Serializable, and List implements scala.Serializable which extends java.io.Serializable):

class DocsService extends Cachable[List[Document]] {
   ...
}

The error message said something like: Cachable[List[Document]] does not conform with type Cachable[A <: java.io.Serializable].

If I understand the issue correctly, the compiler means that Cachable[scala.Serializable] is not a subtype of Cachable[java.io.Serializable].

I thought, <: statement is similar to java's ? extends statement, which would fix this type of problem in Java.

How to fix it in Scala?

1
this trait cannot (reliably) be covariant, because you are both accepting A and returning ADisplay Name

1 Answers

4
votes