为什么这不编译?我在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)