# Generics: in, out, where Classes in Kotlin can have type parameters, just like in Java: ```KOTLIN class Box(t: T) { var value = t } ``` To create an instance of such a class, simply provide the type arguments: ```KOTLIN val box: Box = Box(1) ``` If the compiler can infer the type arguments, for example, from the constructor arguments, you don’t need to specify them explicitly: ```KOTLIN val box = Box(1) // 1 has type Int, so the compiler figures out that it is Box ``` ## Variance One of the trickiest aspects of Java's type system is the wildcard types (see [Java Generics FAQ](http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html)). Kotlin doesn't have these. Instead, Kotlin has declaration-site variance and type projections. ### Variance and wildcards in Java Let's think about why Java needs these mysterious wildcards. First, generic types in Java are invariant, meaning that `List` is not a subtype of `List`. If `List` were not invariant, it would have been no better than Java's arrays, as the following code would have compiled but caused an exception at runtime: ```JAVA // Java List strs = new ArrayList(); // Java reports a type mismatch here at compile-time. List objs = strs; // What if it didn't? // We would be able to put an Integer into a list of Strings. objs.add(1); // And then at runtime, Java would throw // a ClassCastException: Integer cannot be cast to String String s = strs.get(0); ``` Java prohibits such things to guarantee runtime safety. But this has implications. For example, consider the `addAll()` method from the `Collection` interface. What's the signature of this method? Intuitively, you'd write it this way: ```JAVA // Java interface Collection ... { void addAll(Collection items); } ``` But then, you would not be able to do the following (which is perfectly safe): ```JAVA // Java // The following would not compile with the naive declaration of addAll: // Collection is not a subtype of Collection void copyAll(Collection to, Collection from) { to.addAll(from); } ``` That's why the actual signature of `addAll()` is the following: ```JAVA // Java interface Collection ... { void addAll(Collection items); } ``` The wildcard type argument `? extends E` indicates that this method accepts a collection of objects of `E` or a subtype of `E`, not just `E` itself. This means that you can safely read `E`'s from items (elements of this collection are instances of a subclass of E), but cannot write to it as you don't know what objects comply with that unknown subtype of `E`. In return for this limitation, you get the desired behavior: `Collection` is a subtype of `Collection`. In other words, the wildcard with an extends-bound (upper bound) makes the type covariant. The key to understanding why this works is rather simple: if you can only take items from a collection, then using a collection of `String`s and reading `Object`s from it is fine. Conversely, if you can only put items into the collection, it's okay to take a collection of `Object`s and put `String`s into it: in Java there is `List`, which accepts `String`s or any of its supertypes. The latter is called contravariance, and you can only call methods that take `String` as an argument on `List` (for example, you can call `add(String)` or `set(int, String)`). If you call something that returns `T` in `List`, you don't get a `String`, but rather an `Object`. Joshua Bloch, in his book [Effective Java, 3rd Edition](http://www.oracle.com/technetwork/java/effectivejava-136174.html), explains the problem well (Item 31: "Use bounded wildcards to increase API flexibility"). He gives the name Producers to objects you only read from and Consumers to those you only write to. He recommends: Tip: "For maximum flexibility, use wildcard types on input parameters that represent producers or consumers." He then proposes the following mnemonic: PECS stands for Producer-Extends, Consumer-Super. Note: If you use a producer-object, say, `List`, you are not allowed to call `add()` or `set()` on this object, but this does not mean that it is immutable: for example, nothing prevents you from calling `clear()` to remove all the items from the list, since `clear()` does not take any parameters at all. The only thing guaranteed by wildcards (or other types of variance) is type safety. Immutability is a completely different story. ### Declaration-site variance Let's suppose that there is a generic interface `Source` that does not have any methods that take `T` as a parameter, only methods that return `T`: ```JAVA // Java interface Source { T nextT(); } ``` Then, it would be perfectly safe to store a reference to an instance of `Source` in a variable of type `Source` - there are no consumer-methods to call. But Java does not know this, and still prohibits it: ```JAVA // Java void demo(Source strs) { Source objects = strs; // !!! Not allowed in Java // ... } ``` To fix this, you should declare objects of type `Source`. Doing so is meaningless, because you can call all the same methods on such a variable as before, so there's no value added by the more complex type. But the compiler does not know that. In Kotlin, there is a way to explain this sort of thing to the compiler. This is called declaration-site variance: you can annotate the type parameter `T` of `Source` to make sure that it is only returned (produced) from members of `Source`, and never consumed. To do this, use the `out` modifier: ```KOTLIN interface Source { fun nextT(): T } fun demo(strs: Source) { val objects: Source = strs // This is OK, since T is an out-parameter // ... } ``` The general rule is this: when a type parameter `T` of a class `C` is declared `out`, it may occur only in the out-position in the members of `C`, but in return `C` can safely be a supertype of `C`. In other words, you can say that the class `C` is covariant in the parameter `T`, or that `T` is a covariant type parameter. You can think of `C` as being a producer of `T`'s, and NOT a consumer of `T`'s. The `out` modifier is called a variance annotation, and since it is provided at the type parameter declaration site, it provides declaration-site variance. This is in contrast with Java's use-site variance where wildcards in the type usages make the types covariant. In addition to `out`, Kotlin provides a complementary variance annotation: `in`. It makes a type parameter contravariant, meaning it can only be consumed and never produced. A good example of a contravariant type is `Comparable`: ```KOTLIN interface Comparable { operator fun compareTo(other: T): Int } fun demo(x: Comparable) { x.compareTo(1.0) // 1.0 has type Double, which is a subtype of Number // Thus, you can assign x to a variable of type Comparable val y: Comparable = x // OK! } ``` The words in and out seem to be self-explanatory (as they've already been used successfully in C# for quite some time), and so the mnemonic mentioned above is not really needed. It can in fact be rephrased at a higher level of abstraction: [The Existential](https://en.wikipedia.org/wiki/Existentialism) Transformation: Consumer in, Producer out! :-) ## Type projections ### Use-site variance: type projections It is very easy to declare a type parameter `T` as `out` and avoid trouble with subtyping on the use site, but some classes can't actually be restricted to only return `T`'s! A good example of this is `Array`: ```KOTLIN class Array(val size: Int) { operator fun get(index: Int): T { ... } operator fun set(index: Int, value: T) { ... } } ``` This class can be neither co- nor contravariant in `T`. And this imposes certain inflexibilities. Consider the following function: ```KOTLIN fun copy(from: Array, to: Array) { assert(from.size == to.size) for (i in from.indices) to[i] = from[i] } ``` This function is supposed to copy items from one array to another. Let's try to apply it in practice: ```KOTLIN val ints: Array = arrayOf(1, 2, 3) val any = Array(3) { "" } copy(ints, any) // ^ type is Array but Array was expected ``` Here you run into the same familiar problem: `Array` is invariant in `T`, and so neither `Array` nor `Array` is a subtype of the other. Why not? Again, this is because `copy` could have an unexpected behavior, for example, it may attempt to write a `String` to `from`, and if you actually pass an array of `Int` there, a `ClassCastException` will be thrown later. To prohibit the `copy` function from writing to `from`, you can do the following: ```KOTLIN fun copy(from: Array, to: Array) { ... } ``` This is type projection, which means that `from` is not a simple array, but is rather a restricted (projected) one. You can only call methods that return the type parameter `T`, which in this case means that you can only call `get()`. This is our approach to use-site variance, and it corresponds to Java's `Array` while being slightly simpler. You can project a type with `in` as well: ```KOTLIN fun fill(dest: Array, value: String) { ... } ``` `Array` corresponds to Java's `Array`. This means that you can pass an array of `String`, `CharSequence`, or `Object` to the `fill()` function. ### Star-projections Sometimes you want to say that you know nothing about the type argument, but you still want to use it in a safe way. The safe way here is to define such a projection of the generic type, that every concrete instantiation of that generic type will be a subtype of that projection. Kotlin provides so-called star-projection syntax for this: * For `Foo`, where `T` is a covariant type parameter with the upper bound `TUpper`, `Foo<*>` is equivalent to `Foo`. This means that when the `T` is unknown, you can safely read values of `TUpper` from `Foo<*>`. * For `Foo`, where `T` is a contravariant type parameter, `Foo<*>` is equivalent to `Foo`. This means there is nothing you can write to `Foo<*>` in a safe way when `T` is unknown. * For `Foo`, where `T` is an invariant type parameter with the upper bound `TUpper`, `Foo<*>` is equivalent to `Foo` for reading values and to `Foo` for writing values. If a generic type has several type parameters, each of them can be projected independently. For example, if the type is declared as `interface Function` you could use the following star-projections: * `Function<*, String>` means `Function`. * `Function` means `Function`. * `Function<*, *>` means `Function`. Note: Star-projections are very much like Java's raw types, but safe. ### Captured types When you use a type projection, such as `out T` or `in T`, the compiler internally represents the unknown concrete type as a [captured type](https://kotlinlang.org/spec/type-system.html#type-capturing). A captured type is an unknown type with known upper and lower bounds. Captured types are non-denotable, so you can't write them directly in Kotlin code. Instead, you can most frequently see captured types in compiler diagnostics, such as `CapturedType(out X)`. For example, the following type mismatch diagnostic contains a captured type: ```KOTLIN val array: Array = arrayOf("str") val item: Int = array.get(0) // Initializer type mismatch: expected 'Int', actual 'CapturedType(out CharSequence)' ``` The compiler uses the captured type's upper bound for read operations and its lower bound to determine which values are type-safe for write operations: ```KOTLIN // The projected type is Array val array: Array = arrayOf("Kotlin") // The get() read operation uses the captured type's upper bound, CharSequence val item = array.get(0) // The set() write operation uses the captured type's lower bound, Nothing, // which results in an error array.set(0, "New value") // Receiver type 'Array' contains out projection // which prohibits the use of 'fun set(index: Int, value: T): Unit' ``` In this example: * The variable `array` has the projected type `Array`. The compiler represents the projected type argument `out CharSequence` as a captured type with `CharSequence` as its upper bound and `Nothing` as its lower bound. * For the `get()` operation, the compiler approximates the captured type to its upper bound, `CharSequence`, and infers `CharSequence` as the type of `item`. * For the `set()` operation, the captured type has `Nothing` as its lower bound. Since `Nothing` has no instances, writing a value to the projected type isn't type-safe and results in an error. ## Generic functions Classes aren't the only declarations that can have type parameters. Functions can, too. Type parameters are placed before the name of the function: ```KOTLIN fun singletonList(item: T): List { // ... } fun T.basicToString(): String { // extension function // ... } ``` To call a generic function, specify the type arguments at the call site after the name of the function: ```KOTLIN val l = singletonList(1) ``` Type arguments can be omitted if they can be inferred from the context, so the following example works as well: ```KOTLIN val l = singletonList(1) ``` ## Generic constraints The set of all possible types that can be substituted for a given type parameter may be restricted by generic constraints. ### Upper bounds The most common type of constraint is an upper bound, which corresponds to Java's `extends` keyword: ```KOTLIN fun > sort(list: List) { ... } ``` The type specified after a colon is the upper bound, indicating that only a subtype of `Comparable` can be substituted for `T`. For example: ```KOTLIN sort(listOf(1, 2, 3)) // OK. Int is a subtype of Comparable sort(listOf(HashMap())) // Error: HashMap is not a subtype of Comparable> ``` The default upper bound (if there was none specified) is `Any?`. Only one upper bound can be specified inside the angle brackets. If the same type parameter needs more than one upper bound, you need a separate where-clause: ```KOTLIN fun copyWhenGreater(list: List, threshold: T): List where T : CharSequence, T : Comparable { return list.filter { it > threshold }.map { it.toString() } } ``` The passed type must satisfy all conditions of the `where` clause simultaneously. In the above example, the `T` type must implement both `CharSequence` and `Comparable`. ## Definitely non-nullable types To make interoperability with generic Java classes and interfaces easier, Kotlin supports declaring a generic type parameter as definitely non-nullable. To declare a generic type `T` as definitely non-nullable, declare the type with `& Any`. For example: `T & Any`. A definitely non-nullable type must have a nullable [upper bound](#upper-bounds). The most common use case for declaring definitely non-nullable types is when you want to override a Java method that contains `@NotNull` as an argument. For example, consider the `load()` method: ```JAVA import org.jetbrains.annotations.*; public interface Game { public T save(T x) {} @NotNull public T load(@NotNull T x) {} } ``` To override the `load()` method in Kotlin successfully, you need `T1` to be declared as definitely non-nullable: ```KOTLIN interface ArcadeGame : Game { override fun save(x: T1): T1 // T1 is definitely non-nullable override fun load(x: T1 & Any): T1 & Any } ``` When working only with Kotlin, it's unlikely that you will need to declare definitely non-nullable types explicitly because Kotlin's type inference takes care of this for you. ## Type erasure The type safety checks that Kotlin performs for generic declaration usages are done at compile time. At runtime, the instances of generic types do not hold any information about their actual type arguments. The type information is said to be erased. For example, the instances of `Foo` and `Foo` are erased to just `Foo<*>`. ### Generics type checks and casts Due to the type erasure, there is no general way to check whether an instance of a generic type was created with certain type arguments at runtime, and the compiler prohibits such `is`-checks such as `ints is List` or `list is T` (type parameter). However, you can check an instance against a star-projected type: ```KOTLIN if (something is List<*>) { something.forEach { println(it) } // The items are typed as `Any?` } ``` Similarly, when you already have the type arguments of an instance checked statically (at compile time), you can make an `is`-check or a cast that involves the non-generic part of the type. Note that angle brackets are omitted in this case: ```KOTLIN fun handleStrings(list: MutableList) { if (list is ArrayList) { // `list` is smart-cast to `ArrayList` } } ``` The same syntax but with the type arguments omitted can be used for casts that do not take type arguments into account: `list as ArrayList`. The type arguments of generic function calls are also only checked at compile time. Inside the function bodies, the type parameters cannot be used for type checks, and type casts to type parameters (`foo as T`) are unchecked. The only exclusion is inline functions with [reified type parameters](inline-functions.html#reified-type-parameters), which have their actual type arguments inlined at each call site. This enables type checks and casts for the type parameters. However, the restrictions described above still apply for instances of generic types used inside checks or casts. For example, in the type check `arg is T`, if `arg` is an instance of a generic type itself, its type arguments are still erased. ```KOTLIN //sampleStart inline fun Pair<*, *>.asPairOf(): Pair? { if (first !is A || second !is B) return null return first as A to second as B } val somePair: Pair = "items" to listOf(1, 2, 3) val stringToSomething = somePair.asPairOf() val stringToInt = somePair.asPairOf() val stringToList = somePair.asPairOf>() val stringToStringList = somePair.asPairOf>() // Compiles but breaks type safety! // Expand the sample for more details //sampleEnd fun main() { println("stringToSomething = " + stringToSomething) println("stringToInt = " + stringToInt) println("stringToList = " + stringToList) println("stringToStringList = " + stringToStringList) //println(stringToStringList?.second?.forEach() {it.length}) // This will throw ClassCastException as list items are not String } ``` ### Unchecked casts Type casts to generic types with concrete type arguments such as `foo as List` cannot be checked at runtime. These unchecked casts can be used when type safety is implied by the high-level program logic but cannot be inferred directly by the compiler. See the example below. ```KOTLIN fun readDictionary(file: File): Map = file.inputStream().use { TODO("Read a mapping of strings to arbitrary elements.") } // We saved a map with `Int`s into this file val intsFile = File("ints.dictionary") // Warning: Unchecked cast: `Map` to `Map` val intsDictionary: Map = readDictionary(intsFile) as Map ``` A warning appears for the cast in the last line. The compiler can't fully check it at runtime and provides no guarantee that the values in the map are `Int`. To avoid unchecked casts, you can redesign the program structure. In the example above, you could use the `DictionaryReader` and `DictionaryWriter` interfaces with type-safe implementations for different types. You can introduce reasonable abstractions to move unchecked casts from the call site to the implementation details. Proper use of [generic variance](#variance) can also help. For generic functions, using [reified type parameters](inline-functions.html#reified-type-parameters) makes casts like `arg as T` checked, unless `arg`'s type has its own type arguments that are erased. An unchecked cast warning can be suppressed by [annotating](annotations.html) the statement or the declaration where it occurs with `@Suppress("UNCHECKED_CAST")`: ```KOTLIN inline fun List<*>.asListOfType(): List? = if (all { it is T }) @Suppress("UNCHECKED_CAST") this as List else null ``` Note: On the JVM: [array types](arrays.html) (`Array`) retain information about the erased type of their elements, and type casts to an array type are partially checked: the nullability and actual type arguments of the element type are still erased. For example, the cast `foo as Array?>` will succeed if `foo` is an array holding any `List<*>`, whether it is nullable or not. ## Underscore operator for type arguments The underscore operator `_` can be used for type arguments. Use it to automatically infer a type of the argument when other types are explicitly specified: ```KOTLIN abstract class SomeClass { abstract fun execute() : T } class SomeImplementation : SomeClass() { override fun execute(): String = "Test" } class OtherImplementation : SomeClass() { override fun execute(): Int = 42 } object Runner { inline fun , T> run() : T { return S::class.java.getDeclaredConstructor().newInstance().execute() } } fun main() { // T is inferred as String because SomeImplementation derives from SomeClass val s = Runner.run() assert(s == "Test") // T is inferred as Int because OtherImplementation derives from SomeClass val n = Runner.run() assert(n == 42) } ```