tests for variance added; little fixes
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package variance
|
||||
|
||||
abstract class Consumer<in T> {}
|
||||
|
||||
abstract class Producer<out T> {}
|
||||
|
||||
abstract class Usual<T> {}
|
||||
|
||||
fun foo(c: Consumer<Int>, p: Producer<Int>, u: Usual<Int>) {
|
||||
val c1: Consumer<Any> = <error>c</error>
|
||||
val c2: Consumer<Int> = c1
|
||||
|
||||
val p1: Producer<Any> = p
|
||||
val p2: Producer<Int> = <error>p1</error>
|
||||
|
||||
val u1: Usual<Any> = <error>u</error>
|
||||
val u2: Usual<Int> = <error>u1</error>
|
||||
}
|
||||
|
||||
//Arrays copy example
|
||||
class Array<T>(val length : Int) {
|
||||
fun get(index : Int) : T { return null }
|
||||
fun set(index : Int, value : T) { /* ... */ }
|
||||
}
|
||||
|
||||
fun copy1(from : Array<Any>, to : Array<Any>) {}
|
||||
|
||||
fun copy2(from : Array<out Any>, to : Array<in Any>) {}
|
||||
|
||||
fun <T> copy3(from : Array<out T>, to : Array<in T>) {}
|
||||
|
||||
fun copy4(from : Array<out Number>, to : Array<in Int>) {}
|
||||
|
||||
fun f(ints: Array<Int>, any: Array<Any>, numbers: Array<Number>) {
|
||||
copy1<error>(ints, any)</error>
|
||||
copy2(ints, any) //ok
|
||||
copy2<error>(ints, numbers)</error>
|
||||
copy3<Int>(ints, numbers)
|
||||
copy4(ints, numbers) //ok
|
||||
}
|
||||
Reference in New Issue
Block a user