Generics Flashcards
Generics
If the parameters may be inferred, e.g. from the constructor arguments or by some other means, one is allowed to ___ the type arguments.
omit
Generics
Java’s type system has wildcard types, but Kotlin doesn’t have any. Instead it has ___ ___ and ___ ___.
declaration-site variance; type projections
Generics
We can annotate the type parameter ‘T’ of a class ‘Source’ to make sure that it is only returned (produced) from members of ‘Source<t>', and never consumed. To do this we provide the \_\_\_ modifier.</t>
out
Generics
The general rule is: when a type parameter T of a class C is declared ___, it may occur only in ___-position in the members of C, but in return C {Base} can safely be a supertype of C {Derived}.
out
Generics
If C {Base} can safely be a supertype of C {Derived}, then C is also known as being ___ in the parameter T, or that T is a ___ type parameter.
covariant
Generics
The out modifier is called a ___ ___, and since it is provided at the type parameter declaration site, we talk about ___ ___. This is in contrast with Java’s use-site variance where wildcards in the type usages make the types covariant.
variance annotation; declaration-site variance
Generics
In addition to out, Kotlin provides a complementary variance annotation ___. It makes a type parameter ___: it can only be consumed and never produced.
in; contravariant
Generics
When projecting a type with the ___ modifier, we can only call those methods that return the type parameter T.
out
Generics
For Foo, where T is a covariant type parameter with the upper bound TUpper, <tt>Foo{*}</tt> is equivalent to ___. It means that when the T is unknown you can safely read values of TUpper from <tt>Foo{*}</tt>.
Foo
Generics
For Foo<in></in>, where T is a contravariant type parameter, Foo<*> is equivalent to ___. It means there is nothing you can write to Foo<*> in a safe way when T is unknown.
Foo<in></in>
Generics
For Foo<t : tupper></t>, where T is an invariant type parameter with the upper bound TUpper, Foo<*> is equivalent to ___ for reading values and to ___ for writing values.
Foo<out></out>; Foo<in></in>
Generics
Sometimes you want to say that you know nothing about the type argument, but 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 would be a subtype of that projection. Kotlin provides the so called ___ syntax for this.
star-projection
Generics
Type parameters are placed ___ the name of the function.
before
Generics
To call a generic function, specify the type arguments at the call site ___ the name of the function.
after
Generics
The most common type of constraint is an ___ ___ that corresponds to Java’s extends keyword.
upper bound