scala – constant pattern

Constantpatterns Aconstantpatternmatchesonlyitself. Anyliteralmaybeusedasaconstant. For example, 5, true, and “hello” are all constant patterns. Also, any val or singleton object can be used as a constant. For example, Nil, a singleton object,isapatternthatmatchesonlytheemptylist.

 

def describe(x: Any) = x match { case 5 => “five” case true => “truth” case “hello” => “hi!” case Nil => “the empty list” case _ => “something else” }

Leave a comment