about summary refs log tree commit diff
path: root/src/libsyntax/ext/expand.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/libsyntax/ext/expand.rs')
-rw-r--r--src/libsyntax/ext/expand.rs36
1 files changed, 35 insertions, 1 deletions
diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs
index 9afbe1e479d..f9ca84473fb 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+
+        );
+    )
 }";
 }