Parser constraints on Kotlin-ish block macros

What I mean by "Kotlin-ish" is allowing a trailing block after normal parenthesis. Its a fairly common pattern in Kotlin. e.g. subscribe("my event") { block here }.

Currently, macro_rules! is special-cased in the compiler to allow the hanging identifier before the block.

It would be neat to declare a macro like:

macro_rules! bikeshed_lambda_wrapper {
  move $b:block => { move || $b },
  $b:block => { || $b }
}

bikeshed_lambda_wrapper! move {
  println!("Body here");
}
macro_rules! capture {
  ($($capture:id $( = $init:expr)),*) $b:block => {
    {
       $(capture_internal!($capture, $init:expr));*
       move || $b
    }
  }
}

capture!(a, b, c = Arc::clone(&c)) {
  c.do_thing(a, b);
}

My understanding is that currently it is a syntax error to invoke a macro like this so it feels implementable, but I'm not fully sure there are any edge cases.

1 Like