-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nested macros using splitdef/combinedef #10
Comments
The julia> macro foo(ex)
def = ex
while def.head === :macrocall
def = def.args[3]
end
esc(combinedef(splitdef(def)))
end
@foo (macro with 1 method)
julia> @foo @foo f(x) = 1
f (generic function with 1 method) The implementation above is quite frail and doesn't re-insert the |
That's a good idea and I can make it work that way. It feels still a little awkward as I'm creating two separate packages and they have their own macros using What do you think about making |
Like this... it would be non-breaking but a function splitdef(ex::Expr; throw::Bool=true, mod=nothing)
# Auto-expand macro expression if necessary
if ex.head === :macrocall
mod !== nothing || Base.throw(ArgumentError("The `mod` keyword argument must be specified."))
ex = macroexpand(mod, ex)
end
# ..........
end |
You should be able to drop the |
Unfortunately, |
My bad there. I was still thinking about the |
I happened to build two macros for different packages (hence difference purposes) that generate functions using Currently, I can make it work now by just adding a single line of code right before running ex = ex.head === :macrocall ? macroexpand(__module__, ex) : ex A more robust version can probably catch any exception and report back a nice error message. The issue is that it has to be added to both macros and so the logic is repeated.... To be honest, I am not sure if changing |
I happened to build two macros that uses
splitdef
andcombinedef
functions. When I tried to use them together,splitdef
gives an error. Looks like it doesn't expand the inner macro before applying the outer macro. Is this a ExprTools problem or could I fix it some other way? thanksHere's a MWE:
Error as follows:
The text was updated successfully, but these errors were encountered: