This section is just a short introduction to generics; later, we'll cover it in detail.
Generic programming is a style programming that focuses on creating algorithms (and collaterally, data structures) that work on general problems.
The Kotlin way to support generic programming is using type parameters. In a few words, we wrote our code with type parameters and, later on, we pass those types as parameters when we use them.
Let's take, for example, our Oven
interface:
interface Oven { fun process(product: Bakeable) }
An oven is a machine, so we could generalize it more:
interface Machine<T> { fun process(product: T) }
The Machine<T>
interface defines a type parameter T
and a method process(T)
.
Now, we can extend it with Oven
:
interface Oven: Machine<Bakeable>
Now, Oven
is extending Machine
with the Bakeable
type parameter, so the process
method now takes Bakeable
as a parameter.