Scala: pattern matching with reusable condition
问题 Considering this case of pattern matching: foo match { case x if expensiveCalculation(x).nonEmpty => // do something with expensiveCalculation(x) ... } is it possible to "label" or reuse the expensiveCalculation(x) after the => ? Ideally I was expecting something like: foo match { case x val ec = expensiveCalculation(x); if ec.nonEmpty => // do something with ec ... } 回答1: You can write an extractor for the type of x (here assuming InputType ): object Expensive { def unapply(x: InputType):