Comparison between existential types

I have several such classes:

class Value[T]

class Container[T](v: Value[T], x: T)

Now I want to convert a sequence (Value[T], T)to a sequence Container[T], where Tfor each element of the sequence may be different. This type can be expressed existential:

def mapContainer(s: Seq[(Value[T], T) forSome {type T}]): Seq[Container[T] forSome {type T}]

However, it seems that I cannot write the body of this function. My naive attempt failed:

def mapContainer(s: Seq[(Value[T], T) forSome {type T}]) =
    s.map(t => new Container(t._1, t._2))

The error looks like this:

[error]  found   : Value[T]
[error]  required: Value[Any]
[error] Note: T <: Any, but class Var is invariant in type T.

I cannot make covariance Value, and still this will not be the right decision. I suppose.

As I said, I am also well versed in the type of result Seq[Container[_]], but I could not find a way to get it.

Is it possible to express this without resorting to asInstanceOfsuch?

+4
source share
1

:

s.map(t => new Container(t._1, t._2))

s.map { case (v, x) => new Container(v, x) }

, , .

+4

Source: https://habr.com/ru/post/1599851/


All Articles