Search code examples
scalaexistential-type

existential types


(UPDATE: simplified the code and also show why it should work)

How can I fix this code?:

case class Sub[B <: Seq[_] : Manifest](b: B) {
  def foo[B2 >: B <: Seq[_] : Manifest](other: Sub[B2]) : B2 =  {
    println(manifest[B])
    println(manifest[B2])

    // next line doesn't compile 
    // other.b ++ b
    other.b 
  }

}

If I uncomment the line other.b ++ b I get the error:

<console>:13: error: Cannot construct a collection of type That with elements of type Any based on a collection of type Repr.
           other.b ++ b
                   ^

If I comment, the code compiles and running it:

scala> Sub(List(1,2)).foo(Sub(Seq(4,5)))
scala.collection.immutable.List[Int]
scala.collection.Seq[Int]
res0: Seq[Int] = List(4, 5)

So the compiler knows that the elements are of type List[Int] and Seq[Int]. It should have no problem concatenating them.

Note: I want to retain the use of 'B2 >: B' as I need it to be inferred.


Solution

  • If you're not tied to using an existential type, this should work:

    case class Sub[T, B[X] <: Seq[X]](b: B[T]) {
      def foo[B2[X] <: Seq[X]](other: Sub[T,B2]) =
         other.b ++ b
    }