为什么不能访问 reified 类型参数的伴生对象?

问题描述

在下面,由于 T 是具体化的,我想通过访问它的伴随对象来使用它 "almost as if it were a normal class"

class Cls {
  companion object {
    fun method() { }
  }    
}

inline fun <reified T> func() {
  T.method()  // error
}

fun main() {
  func<Cls>()
}

但是失败了

Type parameter 'T' cannot have or inherit a companion object,so it cannot be on the left hand side of dot

因此似乎丢失了大量信息。使用和不使用 reified 时我都会遇到相同的错误。我希望 reified 类型参数比 Java 的实现更全面。我在 C++ 模板方面拥有丰富的经验。

我找到了一些解决方法(使用反射都非常令人失望),但我真的在问为什么这行不通。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)