# Declaration-Site Variance

Published articles for Declaration-Site Variance.

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

## Declaration-Site Variance

DevFeed: [Declaration-Site Variance](<https://devfeed.tech/articles/declaration-site-variance-25012.md>)

Original publisher: [Read original article](<https://typealias.com/concepts/declaration-site-variance/>)

Author: author@typealias.com (Dave Leeds)

Published: 2018-01-17T00: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>)

Tags: [class](<https://devfeed.tech/tags/class.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [covariance](<https://devfeed.tech/tags/covariance.md>), [declaration-site-variance](<https://devfeed.tech/tags/declaration-site-variance.md>), [function](<https://devfeed.tech/tags/function.md>), [generic](<https://devfeed.tech/tags/generic.md>), [interface](<https://devfeed.tech/tags/interface.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [programming](<https://devfeed.tech/tags/programming.md>), [property](<https://devfeed.tech/tags/property.md>), [type-parameter](<https://devfeed.tech/tags/type-parameter.md>), [variance](<https://devfeed.tech/tags/variance.md>)

### AI overview

This tutorial explains Kotlin declaration-site variance, including the in and out annotations, the restrictions they impose on type parameters, and the resulting subtyping relationships such as covariance.

### Source excerpt

Declaration-Site Variance is variance that is specified at the point where the generic is defined - for example, in the definition of the class, interface, function, or extension property. To use declaration-site variance, you just have to modify a type parameter with either the in or out variance annotation. By doing this, you limit where you're allowed to use the type parameter, but you also introduce subtyping for your generic, so that, for example, a Box<Dog> can be a subtype of Box<Animal>, whereas otherwise this wouldn't be possible.

## Type Projection

DevFeed: [Type Projection](<https://devfeed.tech/articles/type-projection-25028.md>)

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

Author: author@typealias.com (Dave Leeds)

Published: 2017-12-27T18:18:01Z

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>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [compiler](<https://devfeed.tech/tags/compiler.md>), [declaration-site-variance](<https://devfeed.tech/tags/declaration-site-variance.md>), [generic](<https://devfeed.tech/tags/generic.md>), [generic-variance](<https://devfeed.tech/tags/generic-variance.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [object](<https://devfeed.tech/tags/object.md>), [operations](<https://devfeed.tech/tags/operations.md>), [programming](<https://devfeed.tech/tags/programming.md>), [type-projection](<https://devfeed.tech/tags/type-projection.md>), [use-site-variance](<https://devfeed.tech/tags/use-site-variance.md>), [variance](<https://devfeed.tech/tags/variance.md>)

### AI overview

This tutorial explains Kotlin type projection as a restricted view of an existing type that enables variance and subtyping in specific contexts. It describes how use-site variance produces projections and how projections can restrict operations such as setters.

### Source excerpt

A type projection is a type that has been limited in certain ways in order to gain variance characteristics. Imagine a three-dimensional object that you shine a flashlight onto. Behind that object on the wall is a two-dimensional projection of that object - a shadow. That projection has the same basic shape as the object, but only from one perspective. Type projection is kind of like that - we take an existing object and reduce it to just the attributes or operations that we need at that place in the code.

## Variance Annotation

DevFeed: [Variance Annotation](<https://devfeed.tech/articles/variance-annotation-25029.md>)

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

Author: author@typealias.com (Dave Leeds)

Published: 2017-12-04T02:49:24Z

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>)

Tags: [class](<https://devfeed.tech/tags/class.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [declaration-site-variance](<https://devfeed.tech/tags/declaration-site-variance.md>), [function](<https://devfeed.tech/tags/function.md>), [generic](<https://devfeed.tech/tags/generic.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [parameter](<https://devfeed.tech/tags/parameter.md>), [programming](<https://devfeed.tech/tags/programming.md>), [subtype](<https://devfeed.tech/tags/subtype.md>), [type-argument](<https://devfeed.tech/tags/type-argument.md>), [types](<https://devfeed.tech/tags/types.md>), [use-site-variance](<https://devfeed.tech/tags/use-site-variance.md>), [variance](<https://devfeed.tech/tags/variance.md>)

### AI overview

This tutorial explains variance annotations in Kotlin generics. It covers the out modifier for covariance, the in modifier for contravariance, and the invariant behavior when no variance annotation is declared.

### Source excerpt

A variance annotation is a modifier applied to a type parameter or type argument of a generic, in order to declare its variance. Kotlin defines two variance annotations in its grammar: out and in. out The out modifier is used to declare a type parameter as being covariant. For example: class Box<out T>(private val item: T) { fun getItem(): T = item } With the out variance annotation declared on the type parameter T in this class, subtyping rules can now be applied to Boxes of different types.