On 19/03/2013 7:01 PM, Kang Seonghoon wrote:
> 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 }
>
One can macroize this somewhat if the verbosity is getting to you, but I
should point out that this also works:
let nkeys = match bms.player {
2 | 3 if isbme => ~"14",
2 | 3 => ~"10",
_ if isbme => ~"7",
_ => ~"5"
};
as does a tuple match, an inverted-nesting, and several other phrasings.
There are a number of equivalent ways of writing conditionals, differing
only in readability. We did actually have a ternary operator for a while
(after people argued for it) but then we removed it (after people argued
against).
> If we had a macro like this:
>
> macro_rules! iif(
> ($cond:expr, $then:expr, $otherwise:expr) =>
> (if $cond { $then } else { $otherwise })
> )
>
I suppose. It's not much shorter (2 chars?) and may also confuse readers
(why have two forms of an if-then-else conditional?)
If we see a lot of people macro-izing it I'd be willing to take the
patch, but so far this is the first time anyone's mentioned wanting it
as a _macro_ (though I guess there was that macro_rules! mini lisp ...
kinda thought that was in jest). In general I think we want to only add
stuff to the core macros if it's getting wide use. They get injected
into _every_ program (at least until we have, say, lazily-loaded and
crate-exported macros, *cough*).
Usually when this comes up it's a call for ternary operator (which is
fully 7 chars shorter) and while I understood and accepted that argument
back the first time it happened, as well as its reversal on the grounds
of simplification, I have a bit of a time-traveller's rule about trying
not to revisit the same design choice three times.
-Graydon
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev