Idea: postfix let as basis for postfix macros

So to summarize and highlight, the "method-position macro part" of the proposal is that instead of a postfix macro expanding to some expression, it should expand to some "method call position" valid tokens.

I honestly think that postfix macros should not be able to rewrite anything outside of their brackets. (Or in other words, the head of the expression is evaluated before entering the macro-generated code.)

If you take that as a given, then using a "postfix match" to write "method-position macros" makes intuitive sense. You could also have macro_rules! macros work both in expression/statement position and "method-call position" by making those separate match arms:

macro_rules! r#try {
    ($expr:expr $(,)?) => { $expr? };
    .() => { .match x { x => $crate::r#try(x) } };
}

However, you can also achieve the same result just by having a temporary introduced as part of the macro call syntax:

macro_rules! r#try {
    ($expr:expr $(,)?) => { $expr? };
    $self.() => { $crate::r#try($self) };
}
3 Likes