Existential types for F-restricted polymorphic types and not common subtypes?

I have two subtypes, which I have to be an F-restricted polymorphic type Aand a subtype of one of these subtypes, i.e.

trait A[T <: A[T]] {
  def x: T
}
trait Ter extends A[Ter]
trait For extends A[For]
trait C extends Ter

Next, I try to implement a specific type

case class F2(l: List[A[_]]) extends For {
  def x: For = F2(l.map(_.x))
}

But this does not compile with:

<console>:11: error: type mismatch;
 found   : List[Any]
 required: List[A[_]]
         def x: For = F2(l.map(_.x))
                              ^

So google says I need to use existential types, which makes sense, so I try:

import scala.language.existentials

type SomeA = T forSome { type T <: A[T] }

case class F1(l: List[SomeA]) extends For {
  def x: For = F1(l.map(_.x))
}

But now I face a new problem when I try to create an instance

trait Example {
  val b: Ter
  val c: C
  val d: For

  // Works fine
  val l1: List[A[_]] = List(b, c, d)
  // But this doesn't work, fails to compile (see below)
  val l2: List[SomeA] = List(b, c, d)

  val f1 = F1(l2)
}

Compilation Error:

<console>:22: error: type mismatch;
 found   : C
 required: SomeA
    (which expands to)  T forSome { type T <: A[T] }
         val l2: List[SomeA] = List(b, c, d)
                                       ^

? , C Ter, , , A[Ter], C A[Ter], T Ter , C A[T], C SomeA.

. c.asInstanceOf[SomeA], . ?

, List[A[_]] , List[SomeA], .. , A[T] T, , A[T] T .

BOUNS. , , , .

+4
1

, . :

val l2 = List[SomeA](b, c: Ter, d)
+1

All Articles