在 kotlin 中使用具有泛型类型的类型别名?

Use a Type Alias with a generic type in kotlin?

目前我使用这个类型别名

typealias GridMatrix = MutableList<CoordsValue, String?>

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

typealias GridMatrix = MutableList<CoordsValue, T?>

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

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

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

试试这个:

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

这是kotlin reference on typealiases