Kotlin Tutorial 13 — Generics

Nick Apperley
3 min readJan 8, 2017

--

Here is the final tutorial in the Kotlin tutorial series which covers a feature that will only be found in some statically typed programming languages, generics. Collection APIs will typically make heavy use of generics since it allows for a single function to handle many different types as if there was a copy of the function defined for each data type. This reduces code duplication and adheres to the DRY principle.

Classes and interfaces can also make use of generics so that a single class/interface can handle multiple data types just like functions. A defined generic type in a function/class/interface can be set for output (out — producer) only, or input (in — consumer) only. In IntelliJ create a new Kotlin project in ~/kotlin_projects called kotlin_tutorial13.

Function Generics

Functions can be generified to handle multiple types. In src create a new Kotlin file called main with the following contents:

Multiple generic types can be defined in a function (in between the <> block), with each one separated by a comma (,). In the code sample above a single generic type called T is defined which is a subtype of Number (a upper bound applied to the type). Notice that the Number type is being specified to the arrayOf function to indicate that an array of numbers should be created. If strings (not a subtype of Number) were passed through as arguments to the combineToString function then a compile error would occur since only subtypes of Number are allowed. Normally generic type inference is applied when using a generified function or class/interface. Run the main.kt file.

Class/Interface Generics

Not only can functions be generified but classes/interfaces can be generified too. Generic types are defined in the <> block just after the class/interface name. In src create a new Kotlin file called Box with the following contents:

As you can see in the code sample above properties can be generified just like classes/interfaces/functions. Append println(“Language Box Item: ${Box(listOf(“Kotlin, Python, Go, Rust, Swift”)).item}”) to the main function in the main.kt file. Note that the generic type doesn’t need to be specified (it is type inferred) when creating an instance of the Box class. Run the main.kt file.

Conclusion

Now with a firm understanding of generics it will be much easier to design and implement APIs, including structuring programs. With all the Kotlin basics in place you are now ready to develop Kotlin programs/libraries. For additional challenges that will improve your understanding of the Kotlin programming language go through the Kotlin Koans, which are a series of gruelling mental exercises that take MANY hours to complete.

Hope you enjoyed going through the Kotlin tutorial series and learning new ways to solve problems from different approaches.

TOC

  1. Kotlin Tutorial 1 — Introduction
  2. Kotlin Tutorial 2 — Basic Data Types
  3. Kotlin Tutorial 3 — Basic Operators
  4. Kotlin Tutorial 4 — Handling Input And Output
  5. Kotlin Tutorial 5 — Basic Collections
  6. Kotlin Tutorial 6 — Control Flow
  7. Kotlin Tutorial 7 — Functions
  8. Kotlin Tutorial 8 — Modularity
  9. Kotlin Tutorial 9 — Using An IDE
  10. Kotlin Tutorial 10 — Classes
  11. Kotlin Tutorial 11 — Inheritance
  12. Kotlin Tutorial 12 — Encapsulation And Polymorphism
  13. Kotlin Tutorial 13 — Generics

--

--

No responses yet