2

I've read here StackOverflow and other places that Scala's immutable Set and the the Key in immutable Map are invariant.

However the following compiles and runs fine in 2.10.0M5

type MapCanvT <: Component with VistaIn
def newMapCanv: MapCanvT
val canv1 = newMapCanv
var vistas = Set[VistaIn](canv1)// Map[VistaIn, Option[CSplit]]((canv1, None))

The Map version that is commented out also compiles and runs fine. This would be a very useful and significant change, that I haven't noticed any documentation for.

Community
  • 1
  • 1
Rich Oliver
  • 6,001
  • 4
  • 34
  • 57

1 Answers1

4

Covariance means that you can do this:

var vistas: Set[VistaIn] = Set[MapCanvT](canv1)

(which you can’t)

var vistas = Set[VistaIn](canv1)

is inferred to

var vistas = Set[VistaIn](canv1: VistaIn)

and thus canv1 fits in nicely because VistaIn is a supertype of MapCanvT.


To answer your question: The Scala 2.10.0 milestone releases still have an invariant Set. (Attention: No stable link.)

Debilski
  • 66,976
  • 12
  • 110
  • 133
  • I see, now you explain it, that makes a lot more sense. I'd avoided Sets because of that misunderstanding. Although I would still imagine its an irritating limitation. – Rich Oliver Aug 21 '12 at 22:26