You can write:
str match { case "foo" | "bar" => ... }
At first glance it looks like |
could be an extractor object, however:
str match { case |("foo", "bar") => ... }
does not work. (And I can't see how that could be implemented anyway.)
So it is a magic built-in operator?
(I believe I have seen this question on SO before, but it's impossible to search for...)
Pattern matching in computer science is the checking and locating of specific sequences of data of some pattern among raw data or a sequence of tokens. Unlike pattern recognition, the match has to be exact in the case of pattern matching.
The second algorithm uses a table which is derived from pattern PAT independent of TEXT. The pattern matching table F(Q,TEXT) of pattern PAT is in memory. The input is an N character string TEXT=T1, T2,…, Tn. This algorithm finds INDEX(PAT) in TEXT.
Pattern matching is a technique where you test an expression to determine if it has certain characteristics. C# pattern matching provides more concise syntax for testing expressions and taking action when an expression matches.
|
is not implemented in the library, it is interpreted by the Scala compiler. It builds a new pattern that is defined as the disjunction between two subpatterns that don't bind any variable (although the newly formed pattern can itself be bound; i.e., you can write stuff like
try { /*...*/ } catch { case e @ (_: IOException | _: IllegalArgumentException) => /*...*/ }
and e
gets as type the most specific supertype of the listed alternatives).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With