From b9824e18c2b02d74bd1bf646fea5f05477ca5071 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Thu, 9 May 2013 01:38:39 +1000 Subject: Add Scheme-style `cond!` macro to syntax::ext::expand Addresses issue #6037 --- src/libsyntax/ext/expand.rs | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 9afbe1e479d..ad1da0a8685 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -542,7 +542,41 @@ pub fn core_macros() -> ~str { } ) - + // + // A scheme-style conditional that helps to improve code clarity in some instances when + // the `if`, `else if`, and `else` keywords obscure predicates undesirably. + // + // # Example + // + // ~~~ + // let clamped = + // if x > mx { mx } + // else if x < mn { mn } + // else { x }; + // ~~~ + // + // Using `cond!`, the above could be written as: + // + // ~~~ + // let clamped = cond!( + // | x > mx { mx } + // | x < mn { mn } + // _ { x } + // ); + // ~~~ + // + // The optional default case is denoted by `_`. + // + macro_rules! cond ( + ($( | $pred:expr $body:block)+ _ $default:block ) => ( + $(if $pred $body else)+ + $default + ); + // for if the default case was ommitted + ( $( | $pred:expr $body:block )+ ) => ( + $(if $pred $body)else+ + ); + ) }"; } -- cgit 1.4.1-3-g733a5