forked from scala/scala3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariances.scala
44 lines (24 loc) · 847 Bytes
/
variances.scala
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
// 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 // (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) { // (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
}