# Contravariance

Published articles for Contravariance.

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.

## Limitations of Generic Variance Modifiers in Java and Kotlin

DevFeed: [Limitations of Generic Variance Modifiers in Java and Kotlin](<https://devfeed.tech/articles/variance-modifiers-limitations-39219.md>)

Original publisher: [Read original article](<https://kt.academy/article/ak-variance-limitations>)

Published: 2023-02-20T00:15:00Z

Content type: tutorial

Language: en

Sources: [Kt. Academy](<https://devfeed.tech/sources/kt-academy.md>)

Topics: [Generic Variance](<https://devfeed.tech/topics/generic-variance.md>), [Covariance](<https://devfeed.tech/topics/covariance.md>), [Contravariance](<https://devfeed.tech/topics/contravariance.md>), [Type Parameter](<https://devfeed.tech/topics/type-parameter.md>), [Java](<https://devfeed.tech/topics/java.md>), [Kotlin](<https://devfeed.tech/topics/kotlin.md>)

Tags: [contravariance](<https://devfeed.tech/tags/contravariance.md>), [covariance](<https://devfeed.tech/tags/covariance.md>), [generic-variance](<https://devfeed.tech/tags/generic-variance.md>), [java](<https://devfeed.tech/tags/java.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [runtime-errors](<https://devfeed.tech/tags/runtime-errors.md>), [type-parameter](<https://devfeed.tech/tags/type-parameter.md>), [variance](<https://devfeed.tech/tags/variance.md>), [workshop-learning-programming](<https://devfeed.tech/tags/workshop-learning-programming.md>)

### AI overview

This tutorial explains how covariance and contravariance affect generic types in Java and Kotlin. It uses Java's covariant arrays to illustrate runtime type errors and describes why Kotlin restricts variance in public in-positions while allowing safe producer and read-only patterns.

### Source excerpt

What limitations generic variance modifiers introduce, and how can we ignore them.

## Kotlin Generic Variance Modifiers

DevFeed: [Kotlin Generic Variance Modifiers](<https://devfeed.tech/articles/kotlin-generic-variance-modifiers-39218.md>)

Original publisher: [Read original article](<https://kt.academy/article/ak-variance>)

Published: 2023-02-08T00:15:00Z

Content type: article

Language: en

Sources: [Kt. Academy](<https://devfeed.tech/sources/kt-academy.md>)

Topics: [Generic Variance](<https://devfeed.tech/topics/generic-variance.md>), [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Contravariance](<https://devfeed.tech/topics/contravariance.md>), [Covariance](<https://devfeed.tech/topics/covariance.md>), [Type Parameter](<https://devfeed.tech/topics/type-parameter.md>)

Tags: [contravariance](<https://devfeed.tech/tags/contravariance.md>), [covariance](<https://devfeed.tech/tags/covariance.md>), [generic-variance](<https://devfeed.tech/tags/generic-variance.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [variance](<https://devfeed.tech/tags/variance.md>), [workshop-learning-programming](<https://devfeed.tech/tags/workshop-learning-programming.md>)

### AI overview

This Kotlin tutorial explains invariant, covariant, and contravariant generic type parameters. It shows how variance affects subtype relationships and why covariance is suitable for read-only types, while mutable structures remain invariant for type safety.

### Source excerpt

What are out and in, and how do we use them without even knowing.

## The Ins and Outs of Generic Variance in Kotlin

DevFeed: [The Ins and Outs of Generic Variance in Kotlin](<https://devfeed.tech/articles/the-ins-and-outs-of-generic-variance-in-kotlin-25037.md>)

Original publisher: [Read original article](<https://typealias.com/guides/ins-and-outs-of-generic-variance/>)

Author: author@typealias.com (Dave Leeds)

Published: 2018-03-14T00: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: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [generics](<https://devfeed.tech/topics/generics.md>), [Compiler](<https://devfeed.tech/topics/compiler.md>)

Tags: [class](<https://devfeed.tech/tags/class.md>), [code](<https://devfeed.tech/tags/code.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [contravariance](<https://devfeed.tech/tags/contravariance.md>), [covariance](<https://devfeed.tech/tags/covariance.md>), [function](<https://devfeed.tech/tags/function.md>), [generic-variance](<https://devfeed.tech/tags/generic-variance.md>), [generics](<https://devfeed.tech/tags/generics.md>), [guide](<https://devfeed.tech/tags/guide.md>), [inheritance](<https://devfeed.tech/tags/inheritance.md>), [interface](<https://devfeed.tech/tags/interface.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [programming](<https://devfeed.tech/tags/programming.md>), [type-parameter](<https://devfeed.tech/tags/type-parameter.md>), [variance](<https://devfeed.tech/tags/variance.md>)

### AI overview

A tutorial explaining generic variance and type projections in Kotlin. It applies two subtype rules to show why covariance and contravariance work and uses a generic animal collection as an example.

### Source excerpt

Have you ever wondered why generic variance works like it does? Or why Kotlin won't let you use a type parameter as an argument when it's marked as out? Have you wondered why the compiler sometimes won't let you call a certain function on a generic? Yes, generics can seem mysterious, but with just two simple, easy-to-understand rules, we can reason our way through almost everything related to variance.

## An Illustrated Guide to Covariance and Contravariance in Kotlin

DevFeed: [An Illustrated Guide to Covariance and Contravariance in Kotlin](<https://devfeed.tech/articles/an-illustrated-guide-to-covariance-and-contravariance-in-kotlin-25035.md>)

Original publisher: [Read original article](<https://typealias.com/guides/illustrated-guide-covariance-contravariance/>)

Author: author@typealias.com (Dave Leeds)

Published: 2018-02-20T00: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: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [generics](<https://devfeed.tech/topics/generics.md>), [Polymorphism](<https://devfeed.tech/topics/polymorphism.md>)

Tags: [article](<https://devfeed.tech/tags/article.md>), [contravariance](<https://devfeed.tech/tags/contravariance.md>), [covariance](<https://devfeed.tech/tags/covariance.md>), [generic-variance](<https://devfeed.tech/tags/generic-variance.md>), [generics](<https://devfeed.tech/tags/generics.md>), [guide](<https://devfeed.tech/tags/guide.md>), [inheritance](<https://devfeed.tech/tags/inheritance.md>), [interface](<https://devfeed.tech/tags/interface.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [programming](<https://devfeed.tech/tags/programming.md>), [variance](<https://devfeed.tech/tags/variance.md>)

### AI overview

This illustrated Kotlin tutorial explains the foundational concepts behind generic variance. It focuses on subtyping, function arguments and results, and how covariance and contravariance appear in class and interface inheritance.

### Source excerpt

Generics can often seem confusing. How often have you started to solve a problem with generics, only to realize that they don't quite work like you thought they did? The good news is that there are some simple, foundational concepts that underpin generic variance. And once you understand those concepts, you won't have to memorize acronyms or resort to trial-and-error - you'll simply understand how and why they work! In this article, I'm going to cover these foundational concepts, and then demonstrate how they play out in Kotlin class and interface inheritance.

## Contravariance

DevFeed: [Contravariance](<https://devfeed.tech/articles/contravariance-25010.md>)

Original publisher: [Read original article](<https://typealias.com/concepts/contravariance/>)

Author: author@typealias.com (Dave Leeds)

Published: 2017-11-17T01:58:28Z

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>), [Polymorphism](<https://devfeed.tech/topics/polymorphism.md>), [Code](<https://devfeed.tech/topics/code.md>), [Java](<https://devfeed.tech/topics/java.md>), [Kotlin](<https://devfeed.tech/topics/kotlin.md>)

Tags: [classes](<https://devfeed.tech/tags/classes.md>), [contravariance](<https://devfeed.tech/tags/contravariance.md>), [generic](<https://devfeed.tech/tags/generic.md>), [generics](<https://devfeed.tech/tags/generics.md>), [inheritance](<https://devfeed.tech/tags/inheritance.md>), [java](<https://devfeed.tech/tags/java.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [programming](<https://devfeed.tech/tags/programming.md>), [type-parameter](<https://devfeed.tech/tags/type-parameter.md>), [types](<https://devfeed.tech/tags/types.md>), [variance](<https://devfeed.tech/tags/variance.md>)

### AI overview

This tutorial explains contravariance as a type relationship in which subtyping proceeds in opposite directions. It uses inheritance and generic classes to show how contravariant type parameters affect subtype relationships, usage, and the ability to return values of the parameter type.

### Source excerpt

Most of the time, subtyping is intuitive. Object-oriented programmers are used to the concept of inheritance, where one class specializes another, and we're familiar with the idea of passing, say, a Dog object where a function expects an Animal. Once you start working with generics, though, there are some fascinating twists that flip our typical understanding of subtyping on its head. What is Contravariance? Contravariance describes a relationship between two sets of types where they subtype in opposite directions.

## Covariance

DevFeed: [Covariance](<https://devfeed.tech/articles/covariance-25011.md>)

Original publisher: [Read original article](<https://typealias.com/concepts/covariance/>)

Author: author@typealias.com (Dave Leeds)

Published: 2017-11-14T00: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>), [Polymorphism](<https://devfeed.tech/topics/polymorphism.md>)

Tags: [classes](<https://devfeed.tech/tags/classes.md>), [contravariance](<https://devfeed.tech/tags/contravariance.md>), [covariance](<https://devfeed.tech/tags/covariance.md>), [generic](<https://devfeed.tech/tags/generic.md>), [generics](<https://devfeed.tech/tags/generics.md>), [inheritance](<https://devfeed.tech/tags/inheritance.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [programming](<https://devfeed.tech/tags/programming.md>), [subtype](<https://devfeed.tech/tags/subtype.md>), [superclass](<https://devfeed.tech/tags/superclass.md>), [variance](<https://devfeed.tech/tags/variance.md>)

### AI overview

This tutorial explains covariance in type systems, focusing on how subtype relationships behave with generic types. It uses Kotlin examples to distinguish covariant read-only types from invariant mutable types, shows how to declare covariance with the out annotation, and discusses related constraints involving function arguments, properties, and inheritance.

### Source excerpt

On the surface, subtypes seem straightforward. Most programmers who write object-oriented code are familiar with the concept of inheritance, and how you can pass off a subclass as if it were its superclass. But subtyping rules aren't always as intuitive when you start working with generics. What is Covariance? Covariance describes a relationship between two sets of types where they both subtype in the same direction. For example, for our first set of types, here are two classes, A and B, where B is a subtype of A: