在Kotlin中使用具有通用类型的类型别名?

问题描述

当前我使用此类型别名

typealias GridMatrix = MutableList<CoordsValue,String?>

现在由于我想这样做,我想用String?替换T?

typealias GridMatrix = MutableList<CoordsValue,T?>

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

是否有办法使它工作?解决方法还是什么?我也不明白为什么不起作用?我想到了typealias就像C ++ DEFINE中那样简单的占位符或替换,它只是在真正的编译开始之前就替换掉了东西。

解决方法

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

尝试一下:

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

这里是kotlin reference on typealiases

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...