avatarGabriel Shanahan

Summary

The provided web content is an article from the "Kotlin Primer" series, focusing on the basics of generics in Kotlin, including generic classes, functions, constraints, type erasure, and the concept of reified type parameters.

Abstract

The article introduces the fundamental concepts of generics in Kotlin, which are essential for creating flexible and reusable code. It explains how generic classes and functions allow developers to write code that can operate on different data types, enhancing code maintainability and reducing duplication. The article also discusses generic constraints, which enable developers to restrict the types that can be used with generics, ensuring type safety and enabling the use of specific methods or properties. Furthermore, it touches on the concept of type erasure, a limitation where generic type information is not available at runtime, similar to Java. The author hints at future lessons that will cover reified type parameters, a Kotlin feature that mitigates some of the issues caused by type erasure. The article is part of a larger series aimed at facilitating Kotlin adoption in Java-centric organizations and was originally created as a learning resource for Etnetera a.s., with gratitude expressed for their support.

Opinions

  • The author expresses that generics are the primary method for generating code programmatically in languages from the C family and highlights Kotlin's enhancements to generic programming, including reified generic types.
  • The article is positioned as an opinionated guide, suggesting that the content reflects the author's perspective and experience with Kotlin, particularly in the context of Java-centric organizations.
  • The author recommends reading the "Introduction" and checking the "Table of Contents" for a comprehensive understanding of the Kotlin Primer series, indicating a structured approach to learning Kotlin.
  • The use of interactive Kotlin Playground examples embedded within the article demonstrates a preference for hands-on learning and immediate application of concepts.
  • The author's gratitude towards Etnetera a.s. for their support implies that the company values continuous learning and professional development in the field of Kotlin programming.

Generics — Basics

The basics of generic classes and generic functions, generic constraints, and type erasure

— — — — — — — — — — — — — — —

THE CURRENT VERSION OF THIS ARTICLE IS PUBLISHED HERE.

— — — — — — — — — — — — — — —

Tags: #FYI++

This article is part of the Kotlin Primer, an opinionated guide to the Kotlin language, which is indented to help facilitate Kotlin adoption inside Java-centric organizations. It was originally written as an organizational learning resource for Etnetera a.s. and I would like to express my sincere gratitude for their support.

It is recommended to read the Introduction before moving on. Check out the Table of Contents for all articles.

In most languages from the C family, generics are the only available language construct for generating code programmatically. They are a way of telling the compiler “take this block of code (class, interface, function) and create a separate copy for every value of a given type parameter”. Kotlin makes changes to more advanced forms of generic programming (which we’ll talk about in the article on variance) and adds a few features of its own (most importantly reified generic types).

As in Java, a generic construct is denoted by <>.

Functions can also be generic:

Generic constraints

Often, we want to place limits on the types that can be used in a generic instance, in order to be able to make certain assumptions (e.g. have certain methods defined). Upper bounds can be specified by:

If more than one upper bound needs to be specified, use where:

Type erasure

As in Java, information about generic types is erased during compile time and is not accessible during runtime. Therefore, there is no general way to check whether an instance of a generic type was created with certain type arguments at runtime, and the compiler prohibits such is-checks.

Type casts to generic types with concrete type arguments, for example, foo as List<String>, cannot be checked at runtime. The compiler issues a warning on unchecked casts.

Reified type parameters, which we will discuss in a future lesson, provide a way to work around one aspect of this problem.

Go back to Nested and Inner Classes, jump to the Table of Contents, or continue to Generic Variance — Motivation.

Join me in Etnetera
Kotlin
Java
Programming
Object Oriented
Generics
Recommended from ReadMedium