Use-site variance: is in and out a sugar for where?

I’m wondering if in and out can always be replaced with where in use-site variance. For example, it seems to me that the following functions are all considered identical.

fun <A,B> f(a: Array<A>, b: Array<B>) where A: B {}
fun <A> g(a: Array<A>, b: Array<in Int>) {}
fun <A> h(a: Array<out A>, b: Array<A>) {}

Given this, can one say that in and out can be desugared into where? (I just want to confirm that my understanding of type system is correct, no intention to compare between ways to write the conditions.)

I’m surprised to say I almost asked “Which language uses ‘where’?” I guess I forgot that Kotlin has a where-clause.

From the docs, the where-clause is used when there is more than one upper bound.

However, having T be required to extend from more than one type does not affect the variance–you’d still have to use in T and out T with your where-clause.

1 Like