Reputation: 41919
Given two Algebraic Data Types: A
and Foo
:
scala> sealed trait A
defined trait A
scala> case object B extends A
defined object B
scala> sealed trait Foo
defined trait Foo
scala> case class FooImpl(x: Int) extends Foo
defined class FooImpl
a simple function f
:
scala> def f: Foo = FooImpl(5)
f: Foo
Finally, I have a few nested match
statements/expressions:
scala> def hoobar(x: A): Int = x match {
| case B(_) => f match {
| case FooImpl(_) => ???
| }
| }
<console>:18: error: object B is not a case class, nor does it have an unapply/unapplySeq member
case B(_) => f match {
^
Why does the above error show up? B
is certainly a case class
, no?
Upvotes: 0
Views: 580
Reputation: 55569
B
is a case object, not a case class. B(_)
doesn't make sense. What would _
be a substitution for when there is only one object B
?
If you want to match on B
, a specific object, use (back-ticks required if lower-case):
case B => ...
Though you could get the other syntax to work by providing an unapply
method for B
, it just doesn't really make sense.
case object B extends A {
def unapply(b: this.type): Option[this.type] = Some(b)
}
Upvotes: 3