blob: d732bb6db298a00895cc4ccb5ab2a3597b0652d3 (
plain) (
blame)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
// Tests variance checking on default methods
import reflect.ClassTag
class Foo[+A: ClassTag](x: A) {
private[this] val elems: Array[A] = Array(x)
def f[B](x: Array[B] = elems): Array[B] = x // error (1) should give a variance error here or ...
}
object Test extends App {
val foo: Foo[Object] = new Foo[String]("A")
val arr = foo.f[Object]()
arr(0) = new Integer(1) // (1) ... will give an ArrayStoreException here
}
class Outer[+A](x: A) {
private[this] var elem: A = x
def getElem: A = elem
class Inner(constrParam: A) { // error (2) should give a variance error here or ...
elem = constrParam
}
}
object Test2 extends App {
val o1: Outer[String] = new Outer[String]("A")
val o2: Outer[Object] = o1
new o2.Inner(new Integer(1))
val x: String = o1.getElem // (2) ... will give a classcast exeption here
}
trait HasY { type Y }
// These are neg-tests corresponding to the pos-test Variances.scala
// where all the variance annotations have been inverted.
trait Foo1[+X] { def bar[Y <: X](y: Y) = y } // error
trait Foo2[+X] { def bar(x: HasY { type Y <: X })(y: x.Y) = y } // error
trait Foo3[-X] { def bar[Y >: X](y: Y) = y } // error
trait Foo4[-X] { def bar(x: HasY { type Y >: X })(y: x.Y) = y } // error
// These are neg-tests corresponding to the pos-test Variances.scala
// where all the bounds have been flipped.
trait Foo5[-X] { def bar[Y >: X](y: Y) = y } // error
trait Foo6[-X] { def bar(x: HasY { type Y >: X })(y: x.Y) = y } // error
trait Foo7[+X] { def bar[Y <: X](y: Y) = y } // error
trait Foo8[+X] { def bar(x: HasY { type Y <: X })(y: x.Y) = y } // error
|