以下代码:
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)
导致以下错误:
...
at Test$.unapply(<console>:13)
at Test$.test(<console>:17)
...
请注意,错误的来源很清楚。但是,错误表明该方法unapply
没有像我想要的那样内联。
我怎样才能内联这个 unapply 方法?这是出于性能原因以及代码重用。