Generics, WTF?
Generics are complicated, and that ties into some esoteric-looking Kotlin syntax that we will explore much later in the book:
- When you see the
out
keyword, such as inclass Something<out T>
, that is Kotlin’s approach for declaring “covariance”, stipulating that an instance ofSomething
is allowed to produceT
objects (e.g., return them from functions) but not consume them (e.g., accept them as parameters) - When you see the
in
keyword, such as inclass Something<in T>
, that is Kotlin’s approach for declaring “contravariance”, stipulating that an instance ofSomething
is allowed to consumeT
objects but not produce them - When you see
<reified T>
… well, that is difficult to explain
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.