Reputation: 1086
Here is an example case class:
case class Person( firstName: Either[Unit, String],
middleName: Either[Unit, Option[String],
lastName: Either[Unit,String])
Any time I get an instance of this case class with a middleName
it is invalid and I want to do something, all other cases are ok.
EDIT
To clarify. I need to guard against using an instance of this case class in a certain method if it was constructed with a middleName
. So I would want to do something like this:
person match {
case Person(_,m,_) => halt()
case _ => continue()
}
I'm just having a hard time thinking about the types involved here.
Upvotes: 0
Views: 217
Reputation: 7466
Your pattern matching does not test the actual value of middleName
, it simply assigns it to m
. All Person
instances are gonna match this first case.
If you want to call halt
if middleName
is a Right
for example you should write:
person match {
case Person(_, Right(_), _) => halt()
case _ => continue()
}
If you want to dive into the value of Right
to see if it's a Some
:
person match {
case Person(_, Right(Some(_)), _) => halt()
case _ => continue()
}
Upvotes: 3