Why doesn't the example compile, aka how does (co-, contra-, and in-) variance work?

后端 未结 4 758
执笔经年
执笔经年 2021-01-21 16:43

Following on from this question, can someone explain the following in Scala:

class Slot[+T] (var some: T) { 
   //  DOES NOT COMPILE 
   //  \"COVARIANT paramete         


        
4条回答
  •  庸人自扰
    2021-01-21 17:17

    You need to apply a lower bound on the parameter. I'm having a hard time remembering the syntax, but I think it would look something like this:

    class Slot[+T, V <: T](var some: V) {
      //blah
    }
    

    The Scala-by-example is a bit hard to understand, a few concrete examples would have helped.

提交回复
热议问题