Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala 2.11 Type Variance Changes

In Scala 2.10.4 this compiles:

trait Foo[-U,T]{
  type Contra = U
}

but in 2.11.0 the same fails with:

contravariant type U occurs in invariant position in type U of type Contra trait Foo[-U,T] {type Contra = U}

Is there a workaround available? Trying to port over a Scala library to 2.11 and the contravariant type is required in order to get a boatload of implicit defs picked up by the compiler (i.e. making U invariant doesn't seem to be an option).

Thanks

like image 475
virtualeyes Avatar asked Apr 30 '14 09:04

virtualeyes


1 Answers

I can't imagine there being a work around available. The reason I say this is all about supporting path-dependent types:

 def foo[T <: Foo[A,B]](that: T): that.Contra

which places the Contra type in the wrong position. You can not return a contravariant type as a result of an operation. Perhaps the search and validation of these requires so much work that the compiler authors decided this small corner case created too much of a burden or it is a compiler bug that you've uncovered.

By the way, this is just wild speculation on my part. I'd have to read the compiler code to figure out which way is which.

like image 163
wheaties Avatar answered Sep 21 '22 19:09

wheaties