class Contains(r: Range) { def unapply(i: Int): Boolean = r contains i } val C1 = new Contains(3 to 10) val C2 = new Contains(20 to 30) scala> 5 match { case C1() => println("C1"); case C2() => println("C2"); case _ => println("none") } C1 scala> 23 match { case C1() => println("C1"); case C2() => println("C2"); case _ => println("none") } C2 scala> 45 match { case C1() => println("C1"); case C2() => println("C2"); case _ => println("none") } none
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter