The error information is clear
for( person <- List(alice, bob, charlie) ) {
person match {
case p @ Person(_, _, Manager) => println("%s is overpaid".format(p.toString))
case p @ Person(_, _, _) => println("%s is underpaid".format(p.toString))
}
}
Here is a short way to do the same thing:
for(p @ Person(_, _, role) <- List(alice, bob, charlie) ) {
if(role == Manager) println("%s is overpaid".format(p.toString))
else println("%s is underpaid".format(p.toString))
}
EDIT
I am not sure what the real mean of id
you wanted, I suppose it is the index
of the person in the list. Here we go:
scala> for((p@Person(_,_,role), id) <- List(alice, bob, charlie).zipWithIndex ) {
| if(role == Manager) printf("%dth person is overpaid\n", id)
| else printf("Something else\n")
| }
Something else
1th person is overpaid
Something else