# Mutable/Immutable collection covariance

**URL:** https://discuss.kotlinlang.org/t/mutable-immutable-collection-covariance/2524
**Category:** Language Design
**Created:** [April 25, 2017, 7:33pm UTC](https://discuss.kotlinlang.org/t/mutable-immutable-collection-covariance/2524 "2017-04-25T19:33:47Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![skovtunenko](https://avatars.discourse-cdn.com/v4/letter/s/e47c2d/32.png) [@skovtunenko](https://discuss.kotlinlang.org/u/skovtunenko)
#### Post date: [April 25, 2017, 7:33pm UTC](https://discuss.kotlinlang.org/t/mutable-immutable-collection-covariance/2524/1 "2017-04-25T19:33:47Z")

</div>

Hello!

It’s a bit unclear to me how does immutable collection covariance work.  
Quote from documentation ([Collections](https://kotlinlang.org/docs/reference/collections.html#collections)):  
`Note that the read only types are covariant. That means, you can take a List<Rectangle> and assign it to List<Shape> assuming Rectangle inherits from Shape. This wouldn't be allowed with the mutable collection types because it would allow for failures at runtime.`

Demo code:

```kotlin
open class Shape
class Rectangle : Shape()

fun main(args: Array<String>) {
    val rectangles: List<Rectangle> = listOf(Rectangle())
    val workedFine: List<Shape> = rectangles // perfectly valid

    val mutableRectangles: MutableList<Rectangle> = mutableListOf(Rectangle())
    val doesntWork: MutableList<Shape> = mutableRectangles // type mismatch
}

```

In Kotlin stdlib sources `MutableList` defined in the same way as read-only `List`.

So how does this different behaviour implemented from technical perspective ? Is this a very special treatment of `immutable\read-only` predefined data types?

---

<div class="post-metadata">

### Author: ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)
#### Post date: [April 25, 2017, 8:23pm UTC](https://discuss.kotlinlang.org/t/mutable-immutable-collection-covariance/2524/2 "2017-04-25T20:23:08Z")

</div>

Here are the sources of kotlin collection interfaces: [kotlin/Collections.kt at 1.1.0 · JetBrains/kotlin · GitHub](https://github.com/JetBrains/kotlin/blob/1.1.0/core/builtins/native/kotlin/Collections.kt)

Notice that the readonly list is declared as  
`public interface List<out E>`  
and the mutable list as  
`public interface MutableList<E>`.

The difference is that type parameter of the former is prefixed with the `out` modifier, which indicates the variance of that type parameter. More about variance [in the documentation](https://kotlinlang.org/docs/reference/generics.html#variance).

---

<div class="post-metadata">

### Author: ![skovtunenko](https://avatars.discourse-cdn.com/v4/letter/s/e47c2d/32.png) [@skovtunenko](https://discuss.kotlinlang.org/u/skovtunenko)
#### Post date: [April 26, 2017, 8:15am UTC](https://discuss.kotlinlang.org/t/mutable-immutable-collection-covariance/2524/3 "2017-04-26T08:15:51Z")

</div>

My bad, you are right, @ilya.gorbunov, thank you!

I missed this `out E` variance part of definition ☹

```kotlin
public interface List<out E> : Collection<E> { .....}
public interface MutableList<E> : List<E>, MutableCollection<E> { .... }

```
