-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Allow macros to apply to modules #48501
Comments
You just need to return your module expression wrapped in a julia> macro foo(mod)
_, name, code = mod.args
pushfirst!(code.args, :(const x = 1))
return esc(Expr(:toplevel, mod))
end
@foo (macro with 1 method)
julia> @foo module Foo
const y = 2
end
Main.Foo
julia> Foo.x
1 |
Ah! Thanks. :) |
You probably know this already, but I thought I’d highlight: When wrapping lots of code in a macro, that macro executes before any nested macro calls. So, for example, my underscore demo would break Chain.jl, whereas if it was a language feature it would execute after macro expansion so it wouldn’t break anything. Many users of Chain.jl put this phenomenon to use when using DataFramesMeta.jl, because |
I didn't see an issue for this, but there very well might be one already.
Occasionally I find myself wanting to transform the language consistently enough that I end up writing:
This works okay but seems clunky compared to being able to simply write "
@mymacro MyModule ...
" which currently errors with "ERROR: syntax: "module" expression not at top level
". Additionally, editors try to insert an extra level of indentation from thebegin
block which is undesired.Some practical examples off the top of my head where applying a macro in this way might be natural:
@inbounds
to an entire module (discussed somewhat in Consider removing--check-bounds=no
? #48245).Downsides to this proposal from the Julia user's point of view (I make no claims as to the implementation difficulty of this idea):
@mymacro begin
block).The text was updated successfully, but these errors were encountered: