The following code:
object Test {
@inline def unapply(i: Int): Option[String] =
i match {
case 1 => Some("Got 1")
case 2 => Some("Got 2")
case 3 => throw new Exception("Should not test 3")
case _ => None
}
def test(i: Int) = i match {
case Test(k) => k
case 4 => "Another 4"
case _ => ""
}
}
Test.test(3)
results in the following error:
...
at Test$.unapply(<console>:13)
at Test$.test(<console>:17)
...
Note that it's clear where the error comes from. However, the error shows that the method unapply
is not inlined as I wanted to.
How can I inline this unapply method? This is for performance reason as well as code reuse.