是否可以在 Position(i, j) case class 和 2D 数组索引之间进行隐式转换?
Is it possible to have an implicit conversion between a Position(i, j) case class and 2D array indices?
假设我有一个 board (
2D 数组)和以下 class:
case class Position(i : Int, j : Int)
典型的场景是这样的:
val v = board(position.i)(position.j)
这让我想知道是否可以通过某种方式在我的 Position
class 和 board
之间定义一个隐式转换,所以我可以写类似
val x = board(position)
谢谢
尝试:
implicit class IndexByPos(board: Array[Array[Int]]) {
def apply(pos: Position) = board(pos.i)(pos.j)
}
不过,我要指出的是,我同意@GuillaumeMassé 的评论 - 您可能会发现为板定义一个特定的 class 更有优势。
假设我有一个 board (
2D 数组)和以下 class:
case class Position(i : Int, j : Int)
典型的场景是这样的:
val v = board(position.i)(position.j)
这让我想知道是否可以通过某种方式在我的 Position
class 和 board
之间定义一个隐式转换,所以我可以写类似
val x = board(position)
谢谢
尝试:
implicit class IndexByPos(board: Array[Array[Int]]) {
def apply(pos: Position) = board(pos.i)(pos.j)
}
不过,我要指出的是,我同意@GuillaumeMassé 的评论 - 您可能会发现为板定义一个特定的 class 更有优势。