August 2017
Intermediate to advanced
440 pages
10h
English
By applying a variance modifier, we gain co-variant/contra-variant behavior for a certain type parameter of the class/interface (declaration-site variance) or type argument (use-site variance). However, there is a limitation that we need to be aware of. To make it safe, the Kotlin compiler limits the positions where type parameters can be used.
With an invariant modifier (default no variance modifier on type parameter) we can use a type parameter on both the in (the type of function parameter) and out (the function return type) positions:
interface Stack<T> { fun push(t:T) // Generic type at in position fun pop():T // Generic type at out position fun swap(t:T):T // Generic type at in and out positions ...Read now
Unlock full access