In Julia, there’s a function do
block syntax like so:
foo() do x
println(x)
end
where foo
is defined like foo(f::Function)
I’m interested in writing a macro that affects a block. i.e. I want user facing code to look something like this:
foo() do x
println(x)
end
but where foo
is defined like this instead: foo(expr::Expr)
I’ve tried writing a macro but this following example doesn’t work (because @foo()
is evaluated first):
julia> @foo() function(x)
println(x)
end
ERROR: syntax: extra token "function" after end of expression
Stacktrace:
[1] top-level scope
@ none:1
And this would work but it is easy for users to accidentally remove the space between @foo
and ()
and that leads to the previous structure which throws an error.
@foo () function(x)
println(x)
end
I’m curious if anyone has suggestions for alternative designs here?