Rust does not have a ternary operator like C's `a ? b : c`, which
corresponds to Rust's `if a { b } else { c }`. I agree that ternary
operators are PITA in many cases, but for simple expressions this
seems a bit verbose: (an excerpt from the actual code)

    let nkeys = match bms.player {
        2 | 3 => if isbme { ~"14" } else { ~"10" },
        _     => if isbme { ~"7"  } else { ~"5"  }
    };
    if haspedal { nkeys + ~"/fp" } else { nkeys }

If we had a macro like this:

    macro_rules! iif(
        ($cond:expr, $then:expr, $otherwise:expr) =>
            (if $cond { $then } else { $otherwise })
    )

the code above could be rewritten as follows:

    let nkeys = match bms.player {
        2 | 3 => iif!(isbme, ~"14", ~"10"),
        _     => iif!(isbme, ~"7",  ~"5")
    };
    iif!(haspedal, nkeys + ~"/fp", nkeys)

This is ultimately a matter of preference, but can be a good addition
to core if this pattern arises a lot and people will end up writing
the same macro.

Some notes:

* `if!` would be the better name, but `if!(x) { y } else { z }` will
be invalid then. I choose `iif!` instead because it is already used in
several languages for expression-level conditionals.
* I suggest not to implement two-argument version `iif!(x, y)` (which
is same as `iif!(x, y, ())` by the definition). We don't want this
form to be used for general blocks.

-- 
-- Kang Seonghoon | Software Engineer, iPlateia Inc. | http://mearie.org/
-- Opinions expressed in this email do not necessarily represent the
views of my employer.
--
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to