scala - Case class value not found -
why accessing case class king in "figures match" informs me case class not found? "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
inside abstract class figure
, not visible outside it.
i guess, trying below
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
now rook
, knight
... figures , inheritance semantics apply them. above code called declaring algebraic datatypes.
now given figure of type figure, can pattern match , find figure exactly.
val figure: figure = rook() figure match { case rook() => "found rook" case _ => "not rook" }
the above code valid. code becomes
abstract class figure //figure can trait 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
based on trying achieve
val listoffigures: list[list[figure]] = .... listoffigures.flatmap { figures => figures.map { figure => figure match { case rook() => "rook logic" case _ => "some other logic" } } }
Comments
Post a Comment