language: Scala (scala-2.10.0)
date: 625 days 8 hours ago
link:
visibility: public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
object Main
{
  def main(args:Array[String]) : Unit = {
    // a silly example class
    class Cov[+A](val value : A) {}
    class Cov2[+A](val1 :A, val value2 : A) extends Cov[A](val1) {
    }
 
    var v1 : Cov[Seq[Int]] = null
    v1 = new Cov(List(1,2,3))
    v1 = new Cov2(List(1), List(2))
    print(v1.value)
 
    class NCov[A](var value : A) {}
    class NCov2[A](val1 :A, var value2 : A) extends NCov[A](val1) {
    }
 
    var v2 : NCov[Seq[Int]] = null
//  v2 = new NCov(List(1,2,3)) //NOPE
    v2 = new NCov(List(1,2,3).asInstanceOf[Seq[Int]])
    v2 = new NCov2(List(1,2,3).asInstanceOf[Seq[Int]], List().asInstanceOf[Seq[Int]])
    v2.value = List(1)
    print(v2.value)
 
 
    var v3 : NCov[T] forSome { type T <: Seq[Int] } = null
    v3 = new NCov(List(1,2,3))
    v3 = new NCov2(List(1), List(2))
//  v3.value = List(1) // NOPE, as in the covariant case
    print(v3.value)
 
    // GEEE!
    var v4 : NCov[T] forSome { type T <: NCov[U] forSome { type U <: Any } } = null
    v4 = new NCov2(new NCov(1), null)
 
    var v5 : Cov[Cov[Any]] = null
    v5 = new Cov(new Cov2(1, 2))
  }
}