首页 文章

Kotlin的@uncheckedVariance?

提问于
浏览
10

在他的演讲Compilers are Databases中,Martin Odersky提出了一个有趣的方差角案例:

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

T 被定义为逆变,因为将类型化的树( Tree[Type] )视为无类型树( Tree[Nothing] )的子类型是有用的,但不是相反 .

通常,Scala编译器会抱怨 T 出现为 tpe 方法的返回类型 . 这就是为什么Martin用 @uncheckedVariance 注释关闭编译器的原因 .

以下是转换为Kotlin的示例:

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

正如预期的那样,Kotlin编译器抱怨 T 出现在'out'位置 . Kotlin有类似于 @uncheckedVariance 的东西吗?或者有更好的方法来解决这个特殊问题吗?

1 回答

  • 9

    Kotlin有一个 @UnsafeVariance 注释,相当于scala中的 @uncheckedVariance

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

相关问题