avatarGabriel Shanahan

Summary

The web content provides an explanation of reified type parameters in Kotlin, detailing their functionality, usage, and common misconceptions.

Abstract

The article "Reified Type Parameters" delves into the concept of reified generics in Kotlin, a feature that allows developers to pass actual types as parameters and use them as if they were concrete types. It explains how reified type parameters work by being inlined at compile time, which is crucial for their operation but limits their use to inline functions. The article also clarifies the distinction between types and values, cautioning against the common mistake of treating a type parameter T as a value. Examples are provided to illustrate the correct usage of reified types with operators like is/as and T::class. The standard library function Iterable<*>.filterIsInstance<T> is highlighted as a notable use case for reified generics. For further exploration, readers are directed to the Kotlin documentation and other articles within the "Kotlin Primer" series.

Opinions

  • The author emphasizes the importance of understanding the distinction between types and values to avoid common mistakes when using reified type parameters.
  • The article is part of a larger educational resource, "The Kotlin Primer," intended to facilitate Kotlin adoption within Java-centric organizations, indicating the author's support for Kotlin as a language of choice for such environments.
  • The author expresses gratitude to Etnetera a.s. for their support in creating the article, suggesting a collaborative effort and endorsement of the company's role in the learning resource's development.
  • By providing a series of Kotlin playground examples, the author demonstrates a practical, hands-on approach to learning, which can be particularly beneficial for readers who prefer interactive learning experiences.
  • The recommendation to read the introduction and the presence of a table of contents implies a structured and comprehensive approach to learning Kotlin, with the author guiding the reader through a curated path of articles.

Reified Type Parameters

An explanation of reified type parameters, how they work, what they can be used for and how it can be tempting to confuse them for values

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

THE CURRENT VERSION OF THIS ARTICLE IS PUBLISHED HERE.

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

Tags: #KOTLIN FEATURE

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.

Going back to generics, a common need when writing certain types of algorithms is to pass an actual type as a parameter. A simple example might be filtering instances of a specific type:

In Java, there is no other way.

In Kotlin, there is:

Declaring a type parameter as reified allows us to use the type as if it were a concrete type (e.g. String, Int etc.) - you can use it with is/as operators, access T::class, etc.

You might be wondering how on earth this works, since all generic type information is thrown away at compile-time. The reason is that the actual value of the type is inlined during compile time, which is also what places the only restriction on reified type parameters - they can only be used in inline functions.

There are a number of functions in the standard library that make use of reified generics, most notably Iterable<*>.filterIsInstance<T>.

Another thing to understand, and a source of common mistakes, is that T is still a type! It is not a value. Here are a couple of tempting scenarios, and how you’re actually supposed to write them:

For a slightly more involved example and some other details, check out the docs.

Go back to Modeling States and Structure: Considerations, jump to the Table of Contents, or continue to Enums.

Join me in Etnetera
Kotlin
Java
Programming
Generics
Reified
Recommended from ReadMedium