2

I'm having trouble with type inference in scala. I'm using classes with higher-kinded types, which extend some traits; but the scala compiler can't resolve the types to the traits they extend. The minimal example is shown here:

trait traitA[X] trait traitB[X] class A[X] extends traitA[X] {} class B extends traitB[C] {} class C {} val a = Seq[A[B]]() val b: Seq[traitA[traitB[C]]] = a Error: type mismatch; found : Seq[A[B]] required: Seq[traitA[traitB[C]]] val b: Seq[traitA[traitB[C]]] = a 

I can get a Seq[traitA[B]] but not a Seq[traitA[traitB[C]]].

What am I missing here?

Thanks for the help

1
  • Works with Seq[A[traitB[C]]](), no? Commented Feb 23, 2018 at 16:49

1 Answer 1

2

You have to ask for covariance in traitA as follows:

trait traitA[+X] 

And you'll get:

scala> val b: Seq[traitA[traitB[C]]] = a res0: Seq[traitA[traitB[C]]] = List() 

Why? because A[X] is a subtype of traitA[X] and you want to enforce that Seq[A[X]] is also a subtype of Seq[traitA[X]].

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.