Handling break
and continue
in trailing macro body #1886
Open
Description
Just wanted to bring this issue I've had up for discussion.
When using trailing macro bodies to implement iteration, break
and continue
don't apply to the macro body and can't be used the same as in a regular for-loop.
Example:
macro @foreach(int[] list; @body(int)) {
foreach (v : list) {
@body(v);
}
}
fn void test() {
@foreach(int[] {1,2,3}; int v) {
break; // Error: There is no valid target for 'break', did you make a mistake?
};
}
Is there some way we can "handle" break
and continue
in a better way to make iteration macros more useful?
I understand there's problems with merely including break in the @body
. Sometimes you have nested loops in the macro and break
should break the outer loop. Or, there may not even be a for loop in the macro.