Have you ever wanted a convenient way to break out of multiple nested loops at once in your Julia code?
The Multibreak package provides the @multibreak
macro, which allows
you to do exactly that. In this contrived but simple example, the
break; break
line breaks out of both loops:
using Multibreak
@multibreak begin
for i = 1:5
if i % 3 > 0
for j = 1:5
@show i, j
if (i + j^2) % 7 == 0
break; break
end
end
end
end
end
More generally the @multibreak
macro allows you to break
out of
any number of nested loops and optionally to continue
the next
enclosing loop.
The tests are the documentation. The tutorial explores the
functionality provided by the @multibreak
macro.
The @multibreak
macro was first implemented as a
gist,
providing a proof of concept for a proposal in the Julia
#5334
issue. The proposed syntax differs by using comma instead of semicolon
between break
/continue
. The former is a syntax error in Julia 1.x,
whereas the latter is syntactically valid but semantically useless,
making it ideal for a macro implementation.