【问题标题】:Use a Type Alias with a generic type in kotlin?在 kotlin 中使用具有泛型类型的类型别名?
【发布时间】:2020-11-02 09:06:55
【问题描述】:

目前我使用这种类型的别名

typealias GridMatrix = MutableList<CoordsValue, String?>

现在我想用T? 替换String?,因为我想这样做

typealias GridMatrix = MutableList<CoordsValue, T?>

不幸的是编译器不允许这样做,因此我有点难过,因为我喜欢这里的 typealias 并且在整个代码库中都使用它。

有没有办法让它发挥作用?解决方法还是什么?我也不明白为什么它不起作用?我认为 typealias 只是一个简单的占位符或替换,就像 C++ 中的 DEFINE 一样,它只是在真正的编译开始之前替换这些东西。

【问题讨论】:

    标签: kotlin type-alias


    【解决方案1】:

    您可以使用类型参数声明类型别名。

    试试这个:

    typealias GridMatrix<T> = MutableList<CoordsValue, T?>
    

    这是kotlin reference on typealiases

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多