Skip to content

Macros #200

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

Closed
wants to merge 2 commits into from
Closed

Macros #200

wants to merge 2 commits into from

Conversation

mdinger
Copy link
Contributor

@mdinger mdinger commented Jul 24, 2014

Japaric's branch: #193. My original PR: #185. I'm hoping the comments will merge onto the end of his...

Target's helping fix this comment. Hopefully it's a little better at showing how repetition works and what is allowed.

@nixpulvis
Copy link

Some of the examples seem repetitive.

@mdinger
Copy link
Contributor Author

mdinger commented Jul 29, 2014

Any recommendations? I could probably turn it into a match like below but I was avoiding matching till the later match example:

#![feature(macro_rules)]

macro_rules! echo {
    // Match a series of expressions separated by `,`; return the same
    // and convert the result into a string. `stringify!` causes
    // evaluation to be skipped.
    ($($x:expr),+) => { stringify!($($x),+) };

    // Same. Whitespace is ignored. This won't run because the first will match.
    //($($x:expr) , +) => { stringify!($($x) , +) };

    // Split on `|`. `stringify` is important here.
    ($($x:expr)|+) => { stringify!($($x)|+) };

    // `$` isn't valid. It's a special macro key.
    ($($x:expr)$+) => { stringify!($($x)$+) };

    // Split on `,` with trailing comma.
    ($($x:expr),+,) => { stringify!($($x),+,) };

    // Split on `any_word`. `stringify` is important here.
    ($($x:expr) any_word +) => { stringify!($($x) any_word +) };

    // Error: only allows one symbol between `$(...)` and `+`
    //($($x:expr),,+) => { stringify!($($x),,+) };
    // Error: only allows one word between `$(...)` and `+`
    //($($x:expr) two words +) => { stringify!($($x) two words +) };
}

fn main() {
    println!("{}", echo!(1u, 2u, 3u));
    // Error: Only a single symbol allowed
    //println!("{}", echo!(1u,, 2u,, 3u));
    println!("{}", echo!(1u | 2u | 3u));
    // Error: not valid. Don't split on `$`.
    //println!("{}", echo!(1u $ 2u $ 3u));
    println!("{}", echo!(1u, 2u, 3u,));
    // Trailing comma              ^
    println!("{}", echo!(1u any_word 2u any_word 3u));
    //Error: Only a single word allowed
    //println!("{}", echo!(1u two words 2u two words 3u));
}

The intent is to highlight that the symbol you split with matters. | might work but $ and + are problematic. Multiple characters (a word) can be used but not multiple words or symbols. It looks a lot like a regex but behaves very differently.

@nixpulvis
Copy link

Hmmm interesting, I like the more concise example you posted, but it would be important to explain matches first. Either of the two paths I'd avoid duplicate examples like:

macro_rules! echo_word {
  // Split on `word`. `stringify` is important here.
  ($($x:expr) word +) => { stringify!($($x) word +) };
}

macro_rules! echo_and {
  // Split on `and`. `stringify` is important here.
  ($($x:expr) and +) => { stringify!($($x) and +) };
}

@mdinger
Copy link
Contributor Author

mdinger commented Jul 29, 2014

and looks like it's a built in keyword which means && (like python's). Same for or and ||. If you don't realize it isn't builtin, you might not expect word to work which is why I originally had both. any_word might be a good compromise.

@@ -1,3 +1,2 @@
bin/*
stage/*
*~
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change seems unrelated

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was because I originally thought automatically hiding gedit temp files might be a good thing. Japaric didn't like it though so I removed it.

@mdinger
Copy link
Contributor Author

mdinger commented Dec 10, 2014

I'll close for now. If I decide to work on it, I'll rebase.

@mdinger mdinger closed this Dec 10, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants