voddan
12/26/2016, 9:08 PMval arr: Array<in Int> = arrayOf(1, 2, 3)
In that line you pledge to use only set
-ing methods like arr[0] = 1
, but not get
-ing methods, e.i. val x = arr[0]
will not compile
val arr: Array<out Int> = arrayOf(1, 2, 3)
Here it is the other way around, you restrict yourself to only use get
and alike.
Declaration-side variance is the same, but you declare from the start what way you can use a type. For example:
class MyList<in T> {...}
That declares that all methods of this class can only intake T
, but not return. Meaning that fun foo(): T
can NOT be a method in this class.
It is similar for out
.
You can declare variance for each generic type: class MyList<in T, out R> {...}
. You are free to mix and match variance however you like, for classes and functions, on declaration and user side.