liveBook · Manning

Chapter 11: Generics

What you will learn in chapter 11 of Kotlin in Action, 2E

The chapter introduces two Kotlin generics concepts: reified type parameters and the distinction between declaration-site and use-site variance.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduces two Kotlin generics concepts: reified type parameters and the distinction between declaration-site and use-site variance.

Key terms: reified type parameters, type arguments, inline function, type erasure, declaration-site variance, use-site variance, type argument, subtype, Java’s wildcards

11.1 Creating types with type arguments: Generic type parameters

Introduces generic type parameters and arguments, shows when Kotlin can infer type arguments, and explains why Kotlin has no raw types.

Key terms: generics, type parameters, type arguments, List<String>, Map<K, V>, type inference, type argument, listOf, raw type, Any!

11.1.1 Functions and properties that work with generic types

This section shows how generic functions and extension properties use type parameters, how the compiler infers those parameters at calls, and why ordinary properties cannot declare them.

Key terms: generic function, type parameter, type argument, List<T>, slice, type inference, filter, (T) -> Boolean, lambda parameter, List<String>

11.1.2 Generic classes are declared with the angle bracket syntax

This subsection shows how to declare generic classes and interfaces and how to supply type arguments when inheriting from generic types.

Key terms: generic class, generic interface, type parameter, angle brackets, type argument, generic parameter, base type, StringList, ArrayList, Comparable

11.1.3 Restricting the type a generic class or function can use: Type parameter constraints

This section explains how upper bounds restrict generic type arguments and make operations on those arguments available inside a generic function.

Key terms: type parameter constraint, upper bound, type argument, subtype, Comparable<T>, max, compareTo, multiple constraints, CharSequence, Appendable

11.1.4 Excluding nullable type arguments by explicitly marking type parameters as non-null

This section explains how to exclude nullable generic type arguments and how to require a non-null value at a specific use site when implementing a Java interface.

Key terms: type parameter, type argument, upper bound, Any?, Any, T : Any, definitely non-nullable, use site, T & Any, nullability annotations

11.2 Generics at run time: Erased and reified type parameters

Introduces JVM type erasure and the possibility of retaining a function’s type arguments at run time with reified type parameters in an inline function.

Key terms: type erasure, type arguments, run time, inline function, reified

11.2.1 Limitations to finding type information of a generic class at run time: Type checks and casts

This section explains how erased generic type arguments limit runtime type checks and casts, while allowing checks when the compiler already knows the type argument.

Key terms: erased, type arguments, run time, List<String>, List<Int>, is checks, star-projection syntax, List<*>, as, as?

11.2.2 Functions with reified type parameters can refer to actual type arguments at run time

Explains how inline functions can reify type parameters so their actual type arguments can be used in run-time checks despite generic type erasure.

Key terms: type erasure, inline function, reified type parameter, type argument, filterIsInstance, List<String>, inline, Java code

11.2.3 Avoiding java.lang.Class parameters by replacing class references with reified type parameters

This section shows how a reified type parameter can make a Java API that requires a java.lang.Class easier to call.

Key terms: reified type parameter, java.lang.Class, ::class.java, ServiceLoader, loadService

11.2.4 Declaring accessors with reified type parameters

This section shows that an inline property accessor with a reified type parameter can refer to the class supplied as its type argument.

Key terms: inline, reified type parameters, property accessor, extension property, canonicalName, T::class.java

11.2.5 Reified type parameters come with restrictions

This section identifies what reified type parameters can and cannot be used for, then explains why their inline-function requirement can make noinline necessary for lambdas.

Key terms: reified type parameter, is, !is, as, as?, ::class, inline functions, lambdas, noinline modifier

11.3 Variance describes the subtyping relationship between generic arguments

This section defines variance as the relationship between generic types with different type arguments and explains why that relationship matters when designing APIs.

Key terms: variance, base type, type arguments, generic classes, generic functions, type-safety expectations

11.3.1 Variance determines whether it is safe to pass an argument to a function

The section uses read-only and mutable lists to show when a list with a more specific element type can safely be passed to a function.

Key terms: List<Any>, List<String>, MutableList<Any>, MutableList<String>, read-only list, type inconsistencies

11.3.2 Understanding the differences between classes, types, and subtypes

This section distinguishes classes from types and uses subtyping to explain assignment, nullability, and the basic relationships between generic types.

Key terms: class, type, type argument, nullable type, subtype, supertype, assignment, function parameter, non-null type, subclass

11.3.3 Covariance preserves the subtyping relation

Explains how Kotlin’s `out` modifier preserves subtyping for a generic class and why the class’s externally visible uses of its type parameter must be restricted to output positions.

Key terms: covariant, subtype, invariant, Herd<T>, out, out position, in position, List<T>, MutableList<T>, constructor parameters

11.3.4 Contravariance reverses the subtyping relation

This section explains how contravariance reverses generic subtyping, why input-only type parameters allow it, and how function types combine contravariance with covariance.

Key terms: contravariance, Consumer<T>, Comparator<T>, subtype, in keyword, in positions, type parameter, (P) -> R, Function1<P, R>, in

11.3.5 Specifying variance for type occurrences via use-site variance

This section explains how Kotlin use-site projections express producer or consumer roles for particular uses of otherwise invariant generic types.

Key terms: declaration-site variance, use-site variance, type projection, MutableList, out projection, source, copyData, in projection, consumer, destination

11.3.6 Star projection: Using the * character to indicate a lack of information about a generic argument

Explains what star projections permit when a generic argument is unknown and how to safely use a container of validators for different types.

Key terms: star projection, MutableList<*>, MutableList<Any?>, out-projected type, Any?, generic type parameter, Consumer<in T>, <in Nothing>, FieldValidator<*>, unchecked cast

11.3.7 Type aliases

This section shows how type aliases make complex type signatures easier to read while explaining why they do not create distinct, type-safe types.

Key terms: type alias, typealias, underlying type, NameCombiner, type safety, inline classes, ValidatedInput

11.4 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 10: Higher-order functions:Lambdas as parameters and return values Chapter 12: Annotations and reflection →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.