Repository navigation
Add Scheme-style cond! macro - #6333
Merged
Merged
Conversation
Contributor
Author
|
@graydon r? |
Contributor
|
nice. Can it use |
Contributor
Author
|
Ahh yes. I tried without the markers, but I get: How would I switch to using |
Contributor
Author
|
@graydon should we close this until those things can be fixed? Or are you happy with having this in the interim? |
Contributor
Author
|
@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.
bors
added a commit
that referenced
this pull request
May 15, 2013
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.
flip1995
pushed a commit
to flip1995/rust
that referenced
this pull request
Dec 6, 2020
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`
U007D
pushed a commit
to U007D/rust-mos
that referenced
this pull request
Aug 21, 2026
6333: Don't interpret type path as part of visibility. r=matklad a=ArifRoktim This closes rust-lang#5902. I only check that the next token isn't equal to `T![:]`, instead of the next two not being equal to `T![::]`. Is that ok? Co-authored-by: Arif Roktim <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Addressing issue #6037, this Scheme-style conditional helps to improve code clarity in instances where the
if,else if, andelsekeywords 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_treemapto 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 reachstage0first.