Using comparison operators in Scala's pattern matching system -
Is it possible to compare Scala using pattern matching system? For example:
A match {Case 10 => Println ("ten") case _ & gt; 10 = & gt; Println ("more than ten") case _ = & gt; The second case statement is invalid, but I want to be able to specify when "when more than one" is printlen ("less than ten")}
After a pattern you have a guard, i.e. a if
and a boolean expression Can add:
one match (case 10 = & gt; Println ("ten") case x if x & gt; 10 = & gt; Println ("more than ten") case _ = & gt; Edit: Println ("less than ten")}
Edit: Note that it is overflowing with a if
after Is different from = & gt;
, because a pattern not matches if the guard is not true.
Comments
Post a Comment