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
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to