# Why are contravariant type parameters in function parameters considered in “out” position?

**URL:** <https://discuss.kotlinlang.org/t/why-are-contravariant-type-parameters-in-function-parameters-considered-in-out-position/12586>\
**Category:** Uncategorized\
**Created:** [May 3, 2019, 11:59pm UTC](https://discuss.kotlinlang.org/t/why-are-contravariant-type-parameters-in-function-parameters-considered-in-out-position/12586 "2019-05-03T23:59:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![hypher](https://avatars.discourse-cdn.com/v4/letter/h/ecd19e/32.png) [@hypher](https://discuss.kotlinlang.org/u/hypher)\
**Post date:** [May 3, 2019, 11:59pm UTC](https://discuss.kotlinlang.org/t/why-are-contravariant-type-parameters-in-function-parameters-considered-in-out-position/12586/1 "2019-05-03T23:59:41Z")

</div>

Also posted at stack overflow: [generics - Why are contravariant type parameters in function parameters considered in "out" position? - Stack Overflow](https://stackoverflow.com/questions/55961416/why-are-contravariant-type-parameters-in-function-parameters-considered-in-out)

It’s a little hard to describe, but basically Kotlin is complaining that a contravariant type paramter is used in an “out” position when it’s used as a contravariant argument to another class:

```
class Consumer<in T> {
    fun consume(t: T) {}
}

class Accepter<in T>() {
    // ERROR: Type parameter T is declared as 'in' but occurs in 'out' position in type Consumer<T>
    fun acceptWith(value: T, consumer: Consumer<T>) {}
}

```

It can be fixed like this:

```
fun <U : T> acceptWith(value: T, consumer: Consumer<U>) {}

```

But I don’t understand the issue. It doesn’t seem unsafe to allow `Consumer<T>`. Can someone explain this?

---

<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:** [May 4, 2019, 4:50am UTC](https://discuss.kotlinlang.org/t/why-are-contravariant-type-parameters-in-function-parameters-considered-in-out-position/12586/2 "2019-05-04T04:50:53Z")

</div>

The argument position is called contravariant because its variance goes in contrary direction w.r.t. the class variance. It means that the supertypes of a class can take the subtypes of an argument type as a parameter and vice versa.

Let’s consider some actual parameter type `S`. In this example a type `Accepter<S>`, which is a supertype of `Accepter<Any>`, must take a _subtype_ of `Consumer<Any>` as a parameter, but with the given signature it takes `Consumer<S>`, that isn’t a subtype of `Consumer<Any>`, but rather a supertype of it.

Another example why that signature would be unsafe if allowed. Let’s consider the following implementations of `Accepter` and `Consumer`:

```auto
class AnyAccepter : Accepter<Any>() {
    override fun acceptWith(value: Any, consumer: Consumer<Any>) {
        consumer.consume(Any())
    }
}

class StringConsumer : Consumer<String>() {
    override fun consume(t: String) {
        println(t.length)
    }
}
fun main() {
    val anyAccepter = AnyAccepter()
    val stringAccepter: Accepter<String> = anyAccepter
    stringAccepter.acceptWith("x", StringConsumer())
}

```

With these implementations you’ll get an unsound program, which will result in ClassCastException at run time:

```auto
Exception in thread "main" java.lang.ClassCastException: class java.lang.Object cannot be cast to class java.lang.String 
	at contravariance.StringConsumer.consume(consumers.kt:27)
	at contravariance.AnyAccepter.acceptWith(consumers.kt:23)
	at contravariance.ConsumersKt.main(consumers.kt:36)

```
