You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This issue was labelled with: B-RFC, I-enhancement in the Rust repository
What Rust calls "pattern guards" are just called "guards" in Haskell. Pattern guards in Haskell allow additional evaluation and a refutable pattern match. If that pattern match fails, it's as if a regular guard returned false. Here's an example, adapted from the HTML5 tokenizer I'm working on.
fn process_char(&mut self, chars: CharSource) {
match self.state {
TagOpen => match chars.next() {
'/' => { self.state = EndTagOpen; }
c if (Some(a) <= c.to_ascii_opt()) => { do_something(a.to_lower()); }
c if other_condition => { ... }
_ => parse_error()
(I'm not advocating for this particular concrete syntax, just trying to get the idea across.)
One can always refactor to avoid the fancy guard, but in general it can produce ugly, hard-to-follow trees of nested matches. In this case I would love to have a single match per tokenizer state which closely follows the specification.
Pattern guards have proven tremendously useful in GHC and were one of the few GHC extensions accepted into Haskell 2010. I think Rust could benefit just as much.
The text was updated successfully, but these errors were encountered:
See this reddit discussion which suggests that the right syntax for this might be to simply extend our if guards to allow if let, just like top-level ifs.
Issue by kmcallister
Tuesday Mar 11, 2014 at 22:08 GMT
For earlier discussion, see rust-lang/rust#12830
This issue was labelled with: B-RFC, I-enhancement in the Rust repository
What Rust calls "pattern guards" are just called "guards" in Haskell. Pattern guards in Haskell allow additional evaluation and a refutable pattern match. If that pattern match fails, it's as if a regular guard returned
false
. Here's an example, adapted from the HTML5 tokenizer I'm working on.(I'm not advocating for this particular concrete syntax, just trying to get the idea across.)
One can always refactor to avoid the fancy guard, but in general it can produce ugly, hard-to-follow trees of nested matches. In this case I would love to have a single match per tokenizer state which closely follows the specification.
Pattern guards have proven tremendously useful in GHC and were one of the few GHC extensions accepted into Haskell 2010. I think Rust could benefit just as much.
The text was updated successfully, but these errors were encountered: