案例 class 未找到值
Case class value not found
为什么在"figures match"中访问案例class King 提示我找不到案例class? "not found: value King"
class Field {
val size: Int = StdIn.readInt()
var matrix: List[List[Boolean]] = List.fill(size, size)(true)
val figures: List[List[Figure]] = List()
def inRange(figures: List[List[Figure]]) = {
figures match {
case King() => field.forEach(matrix) {
if (matrix == true) {
if (Math.abs(m1-m2) <= 1 || Math.abs(n1-n2) <= 1) matrix = false
}
}
}
}
abstract class Figure {
case class Rook()
case class Knight()
case class Bishop()
case class Queen()
case class King()
}
问题是 King
是在摘要 class Figure
中定义的。我猜你要找的是继承。使用 extends
来实现:
abstract class Figure
case class Rook() extends Figure
case class Knight() extends Figure
case class Bishop() extends Figure
case class Queen() extends Figure
case class King() extends Figure
此外,如果您不需要 Rook、Knight 等的任何参数,我建议使用 case object
s 而不是 case class
es
希望对您有所帮助。
King
位于摘要 class Figure
内,在摘要外不可见。
我猜,你正在尝试做如下的事情
trait Figure
case class Rook() extends Figure
case class Knight() extends Figure
case class Bishop() extends Figure
case class Queen() extends Figure
case class King() extends Figure
现在 Rook
、Knight
... 是图形,继承语义适用于它们。上面的代码称为声明代数数据类型。
现在给定一个 Figure 类型的图形,你可以进行模式匹配并找到它到底是什么图形。
val figure: Figure = Rook()
figure match {
case Rook() => "found Rook"
case _ => "Not Rook"
}
以上代码现在有效。您的代码变为
abstract class Figure //Figure can be trait as well.
case class Rook() extends Figure
case class Knight() extends Figure
case class Bishop() extends Figure
case class Queen() extends Figure
case class King() extends Figure
基于您想要实现的目标
val listOfFigures: List[List[Figure]] = ....
listOfFigures.flatMap { figures =>
figures.map { figure =>
figure match {
case Rook() => "Rook logic"
case _ => "Some other logic"
}
}
}
正如 Pamu 所解释的,您希望将图形设置为代数数据类型。您可以将它们设置为 case 对象而不是 case 类 因为您没有任何属性:
trait Figure
case object Rook extends Figure
case object Knight extends Figure
case object Bishop extends Figure
case object Queen extends Figure
case object King extends Figure
然后
val figures: List[List[Figure]] = List()
def inRange(figures: List[List[Figure]]) = {
val f = figures.flatMap(identity)
f.map {
case King => field.forEach(matrix) {
if (matrix == true) {
if (Math.abs(m1 - m2) <= 1 || Math.abs(n1 - n2) <= 1) matrix = false
}
}
}
}
为什么在"figures match"中访问案例class King 提示我找不到案例class? "not found: value King"
class Field {
val size: Int = StdIn.readInt()
var matrix: List[List[Boolean]] = List.fill(size, size)(true)
val figures: List[List[Figure]] = List()
def inRange(figures: List[List[Figure]]) = {
figures match {
case King() => field.forEach(matrix) {
if (matrix == true) {
if (Math.abs(m1-m2) <= 1 || Math.abs(n1-n2) <= 1) matrix = false
}
}
}
}
abstract class Figure {
case class Rook()
case class Knight()
case class Bishop()
case class Queen()
case class King()
}
问题是 King
是在摘要 class Figure
中定义的。我猜你要找的是继承。使用 extends
来实现:
abstract class Figure
case class Rook() extends Figure
case class Knight() extends Figure
case class Bishop() extends Figure
case class Queen() extends Figure
case class King() extends Figure
此外,如果您不需要 Rook、Knight 等的任何参数,我建议使用 case object
s 而不是 case class
es
希望对您有所帮助。
King
位于摘要 class Figure
内,在摘要外不可见。
我猜,你正在尝试做如下的事情
trait Figure
case class Rook() extends Figure
case class Knight() extends Figure
case class Bishop() extends Figure
case class Queen() extends Figure
case class King() extends Figure
现在 Rook
、Knight
... 是图形,继承语义适用于它们。上面的代码称为声明代数数据类型。
现在给定一个 Figure 类型的图形,你可以进行模式匹配并找到它到底是什么图形。
val figure: Figure = Rook()
figure match {
case Rook() => "found Rook"
case _ => "Not Rook"
}
以上代码现在有效。您的代码变为
abstract class Figure //Figure can be trait as well.
case class Rook() extends Figure
case class Knight() extends Figure
case class Bishop() extends Figure
case class Queen() extends Figure
case class King() extends Figure
基于您想要实现的目标
val listOfFigures: List[List[Figure]] = ....
listOfFigures.flatMap { figures =>
figures.map { figure =>
figure match {
case Rook() => "Rook logic"
case _ => "Some other logic"
}
}
}
正如 Pamu 所解释的,您希望将图形设置为代数数据类型。您可以将它们设置为 case 对象而不是 case 类 因为您没有任何属性:
trait Figure
case object Rook extends Figure
case object Knight extends Figure
case object Bishop extends Figure
case object Queen extends Figure
case object King extends Figure
然后
val figures: List[List[Figure]] = List()
def inRange(figures: List[List[Figure]]) = {
val f = figures.flatMap(identity)
f.map {
case King => field.forEach(matrix) {
if (matrix == true) {
if (Math.abs(m1 - m2) <= 1 || Math.abs(n1 - n2) <= 1) matrix = false
}
}
}
}