You could do something like this, just as a proof of concept (not taking elseif
/else
branches into account), similar macros could be done for the other type of blocks:
julia> @eval macro $(:if)(condition, expression, terminator::Symbol)
terminator ≠ :endif && error()
quote
if $condition
$expression
end
end |> esc
end
@if (macro with 1 method)
julia> x = 42
42
julia> @if x == 42 (
info("yep!")
) endif
INFO: yep!
julia> @macroexpand @if x == 42 (
info("yep!")
) endif
quote # REPL[9], line 4:
if x == 42 # REPL[9], line 5:
info("yep!")
end
end
But then again in Julia a lot of control nesting can become a lot of multiple dispatch instead, I would refactor such a long if
block. In Julia you can have pretty much any syntax you like and in a package.
I just wish there where some kind of macro line continuation syntax or semantics, but the parenthesis do the job here (instead of a begin ... end
, which would defeat the purpose of the trailing endif
).
Also you could always do:
if (a=b)
continue
end # if