-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add Scheme-style cond!
macro
#6333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@graydon r? |
nice. Can it use |
Ahh yes. I tried without the markers, but I get:
How would I switch to using |
@graydon should we close this until those things can be fixed? Or are you happy with having this in the interim? |
@graydon ok, I have a possible interim fix for the bad parsing without using the macro_rules! cond(
($(($pred:expr) $body:block)+ _ $default:block) => (
$(if $pred $body )else+
else $default
);
($(($pred:expr) $body:block)+) => (
$(if $pred $body )else+
);
)
fn main() {
let x = 1;
cond!(
(x < 0) { io::println(~"woops"); }
(true ) { io::println(~"hullo"); }
_ { io::println(~"bananas"); }
)
cond!(
(x < 0) { io::println(~"woops"); }
(true ) { io::println(~"hullo"); }
)
} It's not the greatest solution, but it is a backwards compatible fix that could serve us until the macro parser can be improved (or rewritten) to enable: cond! {
x < 0 { io::println(~"woops"); }
true { io::println(~"hullo"); }
_ { io::println(~"bananas"); }
} I definitely think the above syntax is what we should aim for, if macros could ever allow it. |
Addresses issue rust-lang#6037
This is temporary. Once the macro parser has improved or been re-written these can be removed.
Addressing issue #6037, this Scheme-style conditional helps to improve code clarity in instances where the `if`, `else if`, and `else` keywords obscure predicates undesirably. Here is an example: ~~~rust let clamped = if x > mx { mx } else if x < mn { mn } else { x }; ~~~ Using `cond!`, the above could be written as: ~~~rust let clamped = cond!( (x > mx) { mx } (x < mn) { mn } _ { x } ); ~~~ The optional default case is denoted by `_`. I have altered `std::fun_treemap` to demonstrate it in use. I am definitely interested in using it for some of the numeric functions, but I will have to wait for it to reach `stage0` first.
Added lint str_to_string *Please write a short comment explaining your change (or "none" for internal only changes)* changelog: un-deprecate [`str_to_string`] and [`string_to_string`] and introduce them as `restriction` lints again. Fixes rust-lang#5610 Added new lint:- str_to_string r? `@flip1995`
Addressing issue #6037, this Scheme-style conditional helps to improve code clarity in instances where the
if
,else if
, andelse
keywords obscure predicates undesirably.Here is an example:
Using
cond!
, the above could be written as:The optional default case is denoted by
_
.I have altered
std::fun_treemap
to demonstrate it in use. I am definitely interested in using it for some of the numeric functions, but I will have to wait for it to reachstage0
first.