# Kotlin Generics

Published articles for Kotlin Generics.

This is one page of public article previews, not the complete archive. Follow Next page to continue. Summaries are not the original full articles.

## Generic Variance

DevFeed: [Generic Variance](<https://devfeed.tech/articles/generic-variance-25066.md>)

Original publisher: [Read original article](<https://typealias.com/start/kotlin-variance/>)

Author: author@typealias.com (Dave Leeds)

Published: 2024-04-23T00:00:00Z

Content type: tutorial

Language: en

Sources: [Dave Leeds on Kotlin - typealias.com](<https://devfeed.tech/sources/dave-leeds-on-kotlin-typealias-com.md>)

Topics: [generics](<https://devfeed.tech/topics/generics.md>), [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [interfaces](<https://devfeed.tech/topics/interfaces.md>)

Tags: [advanced-kotlin-concepts](<https://devfeed.tech/tags/advanced-kotlin-concepts.md>), [contravariance](<https://devfeed.tech/tags/contravariance.md>), [covariance](<https://devfeed.tech/tags/covariance.md>), [extend](<https://devfeed.tech/tags/extend.md>), [generic-variance](<https://devfeed.tech/tags/generic-variance.md>), [generics](<https://devfeed.tech/tags/generics.md>), [implement](<https://devfeed.tech/tags/implement.md>), [interfaces](<https://devfeed.tech/tags/interfaces.md>), [introduction](<https://devfeed.tech/tags/introduction.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [kotlin-generics](<https://devfeed.tech/tags/kotlin-generics.md>), [kotlin-type-safety](<https://devfeed.tech/tags/kotlin-type-safety.md>), [learn-to-program](<https://devfeed.tech/tags/learn-to-program.md>), [programming](<https://devfeed.tech/tags/programming.md>), [programming-language-type-systems](<https://devfeed.tech/tags/programming-language-type-systems.md>), [software-development](<https://devfeed.tech/tags/software-development.md>), [subtyping-in-kotlin](<https://devfeed.tech/tags/subtyping-in-kotlin.md>), [type-parameters](<https://devfeed.tech/tags/type-parameters.md>)

### AI overview

A tutorial on variance in Kotlin generic types. It explains how covariance and contravariance affect subtype relationships between parameterized types, using examples to show when one generic type can substitute for another.

### Source excerpt

We've seen how we can implement interfaces and extend classes to create subtypes. However, with generic types, we sometimes want to create a subtype based on the type arguments instead of the base types. Kotlin gives us a few different tools to achieve this, and in this chapter we'll learn all about them. In order to understand how this is possible, we'll first need to understand the principles of variance. Then, we'll look at three simple rules that explain when one parameterized type can be the subtype of another.

## Generics

DevFeed: [Generics](<https://devfeed.tech/articles/generics-25057.md>)

Original publisher: [Read original article](<https://typealias.com/start/kotlin-generics/>)

Author: author@typealias.com (Dave Leeds)

Published: 2024-03-26T00:00:00Z

Content type: tutorial

Language: en

Sources: [Dave Leeds on Kotlin - typealias.com](<https://devfeed.tech/sources/dave-leeds-on-kotlin-typealias-com.md>)

Topics: [generics](<https://devfeed.tech/topics/generics.md>), [Kotlin](<https://devfeed.tech/topics/kotlin.md>)

Tags: [classes](<https://devfeed.tech/tags/classes.md>), [collections](<https://devfeed.tech/tags/collections.md>), [enum-class](<https://devfeed.tech/tags/enum-class.md>), [function](<https://devfeed.tech/tags/function.md>), [generics](<https://devfeed.tech/tags/generics.md>), [introduction](<https://devfeed.tech/tags/introduction.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [kotlin-generic-function](<https://devfeed.tech/tags/kotlin-generic-function.md>), [kotlin-generics](<https://devfeed.tech/tags/kotlin-generics.md>), [kotlin-parameterized-types](<https://devfeed.tech/tags/kotlin-parameterized-types.md>), [kotlin-type-argument](<https://devfeed.tech/tags/kotlin-type-argument.md>), [kotlin-type-erasure](<https://devfeed.tech/tags/kotlin-type-erasure.md>), [kotlin-type-parameter](<https://devfeed.tech/tags/kotlin-type-parameter.md>), [kotlin-type-parameter-constraint](<https://devfeed.tech/tags/kotlin-type-parameter-constraint.md>), [kotlin-type-safety](<https://devfeed.tech/tags/kotlin-type-safety.md>), [learn-to-program](<https://devfeed.tech/tags/learn-to-program.md>), [list](<https://devfeed.tech/tags/list.md>), [map](<https://devfeed.tech/tags/map.md>), [pair](<https://devfeed.tech/tags/pair.md>), [programming](<https://devfeed.tech/tags/programming.md>)

### AI overview

A tutorial introducing Kotlin generics through collection types such as List, Pair, and Map, using a beverage-menu example with classes and an enum class.

### Source excerpt

All the way back in Chapter 8, when we introduced collections, we saw our first generic type: List<String> ... then we saw more generics in Chapter 9 when we looked at the Pair and Map classes. In order to stay focused on learning about collection types, we glossed over the details about these classes. Well, we've put off learning about them for long enough! It's finally time for us to gain a full understanding of generics, so buckle up!

## Using @JvmSuppressWildcards to resolve Kotlin and Java generic type issues

DevFeed: [Using @JvmSuppressWildcards to resolve Kotlin and Java generic type issues](<https://devfeed.tech/articles/jvmsuppresswildcards-the-secret-sauce-to-your-sandwich-style-generics-25906.md>)

Original publisher: [Read original article](<https://chao2zhang.medium.com/jvmsuppresswildcards-the-secret-sauce-to-your-sandwich-style-generics-b0093aa5979d?source=rss-d19045640fe------2>)

Author: Chao Zhang

Published: 2021-02-23T16:01:15Z

Content type: tutorial

Language: en

Sources: [Stories by Chao Zhang on Medium](<https://devfeed.tech/sources/stories-by-chao-zhang-on-medium.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [generics](<https://devfeed.tech/topics/generics.md>), [Java](<https://devfeed.tech/topics/java.md>), [Android](<https://devfeed.tech/topics/android.md>), [Dagger](<https://devfeed.tech/topics/dagger.md>), [recyclerview](<https://devfeed.tech/topics/recyclerview.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [android-app-development](<https://devfeed.tech/tags/android-app-development.md>), [compilation-error](<https://devfeed.tech/tags/compilation-error.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [dagger](<https://devfeed.tech/tags/dagger.md>), [generics](<https://devfeed.tech/tags/generics.md>), [java](<https://devfeed.tech/tags/java.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [kotlin-generics](<https://devfeed.tech/tags/kotlin-generics.md>), [recyclerview](<https://devfeed.tech/tags/recyclerview.md>)

### AI overview

This tutorial explains how Kotlin generics interact with Java in mixed Kotlin-Java codebases, including Android applications and Dagger-based dependencies. It shows how the @JvmSuppressWildcards annotation can resolve compilation and type-inference problems when Kotlin generic APIs are consumed by Java code.

### Source excerpt

Photo: amirali mirhashemian from Unsplash If generic types are exposed in Kotlin API, consider @JvmSuppressWildcards so that your Java consumer can compile successfully. Kotlin generics are different from (in my opinion, smarter than) Java generics. Kotlin generics has declaration-site variance and type projections, which are officially documented here. Generics is a gigantic topic itself. My usual practice is to leverage IDE warnings and compiler error messages to fix my code since they usually contain the necessary information to show you where you did wrong. However, as we are Kotlinizing our codebase from Java to Kotlin, it is possible to have a sandwich-style code dependency that mixes Java code and Kotlin code. In such cases, when dealing with generics, your compiler and IDE may not be smart enough to help you fix those issues. What is sandwich-style code dependency?🥪 Sandwich-style Kotlin conversion At a certain time during your Java to Kotlin conversion, there could be some Java code depending on Kotlin code, which in turn depends on Java code. A typical example on Android is that your app code in Java depending on your library in Kotlin, which depends on Android SDK in Java. Even if your app code is written purely in Kotlin, the sandwich might still exist: The Java-based annotation processing Dagger, depends on your app code in Kotlin, which depends on Android SDK in Java. Now let's look at how the secret sauce @JvmSuppressWildcards can help us by making our generic sandwich tasty. Sandwich: List<Object> in RecyclerView methodshttps://medium.com/media/87fd0c63db2b9c82af2b241bf61cef31/href With the code above, the following compilation error is observed: MyChildAdapter.java:13: error: name clash: onBindViewHolder(MyViewHolder,int,List<Object>) in MyChildAdapter overrides a method whose erasure is the same as another method, yet neither overrides the other The error message leads us to think that we need to change our type here: Change the Kotlin generic para