Generics Flashcards

1
Q

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.

A

omit

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Generics

Java’s type system has wildcard types, but Kotlin doesn’t have any. Instead it has ___ ___ and ___ ___.

A

declaration-site variance; type projections

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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>

A

out

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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}.

A

out

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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.

A

covariant

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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.

A

variance annotation; declaration-site variance

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Generics

In addition to out, Kotlin provides a complementary variance annotation ___. It makes a type parameter ___: it can only be consumed and never produced.

A

in; contravariant

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Generics

When projecting a type with the ___ modifier, we can only call those methods that return the type parameter T.

A

out

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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>.

A

Foo

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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.

A

Foo<in></in>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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.

A

Foo<out></out>; Foo<in></in>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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.

A

star-projection

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Generics

Type parameters are placed ___ the name of the function.

A

before

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Generics

To call a generic function, specify the type arguments at the call site ___ the name of the function.

A

after

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Generics

The most common type of constraint is an ___ ___ that corresponds to Java’s extends keyword.

A

upper bound

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Generics

If the same type parameter needs more than one upper bound, we need a separate ___-clause.

A

where

17
Q

Generics

At runtime, the instances of generic types do not hold any information about their actual type arguments. The type information is said to be ___.

A

erased