It says in Scala, pattern-matching will be converted to an invocation of unapply(...):Option[...]
.
Here I defined an object with unapply
:
object Hello {
def unapply(s:String): Option[String] = Some(s)
}
Then I can use it in pattern-matching:
"hello" match {
case Hello(s) => println(s)
}
Actually(maybe) it will be converted to:
Hello.unapply("hello") match {
case Some(s) => println(s)
case _ =>
}
But the Some
here is also a case class
, so the Some.unapply
will be called.
That should be infinite. Which is not possible.
So how do Scala pattern matching the Option
in the underlying?