How can I pattern match on a range in Scala?
问题 In Ruby I can write this: case n when 0...5 then "less than five" when 5...10 then "less than ten" else "a lot" end How do I do this in Scala? Edit: preferably I'd like to do it more elegantly than using if . 回答1: Inside pattern match it can be expressed with guards: n match { case it if 0 until 5 contains it => "less than five" case it if 5 until 10 contains it => "less than ten" case _ => "a lot" } 回答2: class Contains(r: Range) { def unapply(i: Int): Boolean = r contains i } val C1 = new