I attempted to implement unapply
for the class, Foo
:
object Foo {
def unapply(x: Int): Option[Int] = Some(x)
}
class Foo(x: Int)
However, it fails in the REPL when I attempt to use it:
scala> val f = new Foo(100)
scala> f match { case Foo(x) => x }
<console>:13: error: pattern type is incompatible with expected type;
found : Int
required: Foo
f match { case Foo(x) => x }
Why can't I use the unapply
that I created for this example?