Robby Pelssers
unread,Nov 14, 2012, 8:55:16 AM11/14/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to scala...@googlegroups.com
Hi all,
Suppose I have following case classes. I know I can do pattern matching to filter them out but it seems like a lot of boiler plate code.
Is there a more elegant way to accomplish this? I also know I can use isInstanceOf but that does not seem nice either.
Robby
object Test {
trait Move
case class Empty(glass: Int) extends Move
case class Fill(glass: Int) extends Move
case class Pour(from: Int, to: Int) extends Move
val moves = List(Empty(3), Fill(4), Empty(2), Pour(1,3), Fill(8))
//> List(Empty(3), Fill(4), Empty(2), Pour(1,3), Fill(8))
/** I'm only interested in the Fill moves **/
moves.filter(move => move match {
case Empty(n) => false
case Fill(n) => true
case Pour(n,o) => false
}) //> List(Fill(4), Fill(8))
}