小编Lan*_*ter的帖子

在Kotlin中实例化通用数组

为什么这不编译?我在3行中得到编译错误

不能使用T作为reified类型参数.请改用类

class Matrix2d<T>(val rows: Int, val cols: Int, init: (Int, Int) -> T) {

   var data = Array(rows * cols, { i ->
      val r = Math.floor(i.toDouble() / cols).toInt()
      init(r, i - r * cols)
   })

   operator fun get(row: Int, col: Int): T = data[row * cols + col]

   operator fun set(row: Int, col: Int, v: T) = {
      data[row * cols + col] = v
   }
}
Run Code Online (Sandbox Code Playgroud)

我添加了一个工厂函数,它看起来像第二个构造函数,但是在内联函数中实现

class Matrix2d<T>(val rows: Int, val cols: Int, private val …
Run Code Online (Sandbox Code Playgroud)

generics kotlin

13
推荐指数
1
解决办法
5934
查看次数

标签 统计

generics ×1

kotlin ×1