Re: [rust-dev] If and pattern match
I agree that is a macro like your `if_matches` examples were possible, it would be a reasonable solution, including conferring the right scope for `a` and `b`. It is completely unclear to me how to implement it though. What does "use_more_macros line" mean? Thanks, Oren. On Mon, Sep 23, 2013 at 2:06 PM, Huon Wilson wrote: > On 23/09/13 20:52, Jason Fager wrote: > > Doesn't seem like enough bang for the buck to me. In your first example you > save 3 vertical lines but get a really wide one in return, and lose some > indentation > levels but add more syntax and conceptual overhead to the language. > > Might be my lack of imagination, but the feature doesn't seem to expand > out to many other use cases, either. > > Your second case you could write as: > > let foo = get_option("foo"); > let bar = get_option("bar"); > if foo.is_some() && bar.is_some() { > use(foo.unwrap(), bar.unwrap()); > } > > > It's also possible to write a `matches` macro: > > macro_rules! matches { > ($e:expr ~ $($p:pat)|*) => { > match $e { > $($p)|* => true, > _ => false > } > } > } > > fn main() { > let a = Some(1); > let b = Some(2); > > if matches!((a,b) ~ (Some(_), Some(_))) { > println("whatever"); > } > } > > (This has the drawback that accidentally (or otherwise) using a pattern > that always matches, e.g. `matches!((a,b) ~ (_,_))` gives a error message > about the `_ => false` arm being unreachable, which isn't particularly > intuitive.) > > Following the use-more-macros line, one could modify the above to give > something like > > if_matches!(foo ~ (Some(a), Some(b)) => { > // use a, b > }) > > by adding an $expr argument to use instead of `true` and replacing the > false arm with `{}`. Note: this *may* break when match-var-hygiene is > implemented (https://github.com/mozilla/rust/issues/9384), I'm not sure. > > Huon > > > ___ > Rust-dev mailing list > Rust-dev@mozilla.org > https://mail.mozilla.org/listinfo/rust-dev > > ___ Rust-dev mailing list Rust-dev@mozilla.org https://mail.mozilla.org/listinfo/rust-dev
Re: [rust-dev] If and pattern match
On 23/09/13 20:52, Jason Fager wrote: Doesn't seem like enough bang for the buck to me. In your first example you save 3 vertical lines but get a really wide one in return, and lose some indentation levels but add more syntax and conceptual overhead to the language. Might be my lack of imagination, but the feature doesn't seem to expand out to many other use cases, either. Your second case you could write as: let foo = get_option("foo"); let bar = get_option("bar"); if foo.is_some() && bar.is_some() { use(foo.unwrap(), bar.unwrap()); } It's also possible to write a `matches` macro: macro_rules! matches { ($e:expr ~ $($p:pat)|*) => { match $e { $($p)|* => true, _ => false } } } fn main() { let a = Some(1); let b = Some(2); if matches!((a,b) ~ (Some(_), Some(_))) { println("whatever"); } } (This has the drawback that accidentally (or otherwise) using a pattern that always matches, e.g. `matches!((a,b) ~ (_,_))` gives a error message about the `_ => false` arm being unreachable, which isn't particularly intuitive.) Following the use-more-macros line, one could modify the above to give something like if_matches!(foo ~ (Some(a), Some(b)) => { // use a, b }) by adding an $expr argument to use instead of `true` and replacing the false arm with `{}`. Note: this *may* break when match-var-hygiene is implemented (https://github.com/mozilla/rust/issues/9384), I'm not sure. Huon ___ Rust-dev mailing list Rust-dev@mozilla.org https://mail.mozilla.org/listinfo/rust-dev
Re: [rust-dev] If and pattern match
Doesn't seem like enough bang for the buck to me. In your first example you save 3 vertical lines but get a really wide one in return, and lose some indentation levels but add more syntax and conceptual overhead to the language. Might be my lack of imagination, but the feature doesn't seem to expand out to many other use cases, either. Your second case you could write as: let foo = get_option("foo"); let bar = get_option("bar"); if foo.is_some() && bar.is_some() { use(foo.unwrap(), bar.unwrap()); } On Monday, September 23, 2013, Oren Ben-Kiki wrote: > A question / proposed syntax... How about allowing writing something like: > > if (Some(foo), Some(bar)) ~~ (get_option("foo"), get_option("bar)) { > use(foo, bar); > } > > Instead of having to write the more combersome: > > match (get_option("foo"), get_option("bar")) { > (Some(foo), Some(bar)) => { > use(foo, bar); > }, > _otherwise => {}, > } > > Not to mention: > > let foo_bar: bool = (Some(_foo), Some(_bar)) ~~ (get_option("foo"), > get_option("bar")); > > match (get_option("foo"), get_option("bar")) { > (Some(foo), Some(bar)) => { > use(foo, bar); > }, > _otherwise => {}, > } > > Instead of the very cumbersome: > > let foo_bar: bool = > match (get_option("foo"), get_option("bar")) { > (Some(_foo), Some(_bar)) => true, > _otherwise => false, > } > } > > So, in general allow `pattern ~~ expression` to be a boolean expression > and if it is used in an "if" statement allow it to introduce the matched > variables to the "then" scope. > > The operator doesn't have to be ~~, it could be anything unique (though > using ~ for matching has a lot of precedence in other languages). > > Thoughts? > > Oren Ben-Kiki > ___ Rust-dev mailing list Rust-dev@mozilla.org https://mail.mozilla.org/listinfo/rust-dev