Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@uncheckedVariance in Kotlin?

In his talk Compilers are Databases, Martin Odersky presents an interesting variance corner case:

class Tree[-T] {
  def tpe: T @uncheckedVariance
  def withType(t: Type): Tree[Type]
}

T is defined to be contravariant, because it is useful to think of a typed tree (Tree[Type]) as a subtype of an untyped tree (Tree[Nothing]), but not the other way around.

Normally, the Scala compiler would complain about T appearing as the return type of the tpe method. That's why Martin shuts up the compiler with an @uncheckedVariance annotion.

Here is the example translated to Kotlin:

abstract class Tree<in T> {
    abstract fun tpe(): T
    abstract fun withType(t: Type): Tree<Type>
}

As expected, the Kotlin compiler complains about T appearing in an 'out' position. Does Kotlin have something similar to @uncheckedVariance? Or is there a better way to solve this particular problem?

like image 209
fredoverflow Avatar asked Apr 11 '16 20:04

fredoverflow


1 Answers

Kotlin has an @UnsafeVariance annotation which is an equivalent to @uncheckedVariance in scala:

abstract class Tree<in T> {
  abstract fun tpe(): @UnsafeVariance T
  abstract fun withType(t: Type): Tree<Type>
}
like image 163
Vladimir Mironov Avatar answered Sep 18 '22 19:09

Vladimir Mironov