In Julia v0.6 and later, command macros are supported in addition to regular string macros. A command macro invocation like
mymacro`xyz`
gets parsed as the macro call
@mymacro_cmd "xyz"
Note that this is similar to string macros, except with _cmd
instead of _str
.
We typically use command macros for code, which in many languages frequently contains "
but rarely contains `
. For instance, it is fairly straightforward to reimplement a simple version of quasiquoting using command macros:
macro julia_cmd(s)
esc(Meta.quot(parse(s)))
end
We can use this macro either inline:
julia> julia`1+1`
:(1 + 1)
julia> julia`hypot2(x,y)=x^2+y^2`
:(hypot2(x,y) = begin # none, line 1:
x ^ 2 + y ^ 2
end)
or multiline:
julia> julia```
function hello()
println("Hello, World!")
end
```
:(function hello() # none, line 2:
println("Hello, World!")
end)
Interpolation using $
is supported:
julia> x = 2
2
julia> julia`1 + $x`
:(1 + 2)
but the version given here only allows one expression:
julia> julia```
x = 2
y = 3
```
ERROR: ParseError("extra token after end of expression")
However, extending it to handle multiple expressions is not difficult.