Is it possible to directly import a macro in Julia?

Viewed 302

I would like to load a single macro from a package without including all of the other parts of the package. Just like how a specific function can be imported, like:

using Package1: function1 

The reason why I would like this, is not only to load solely the desired part of the package, but also to show explicitly where the macro is loaded from.

1 Answers

Yes, it is possible. Any top-level variable in a module or package can be imported.

julia> module A
           x = 1
           
           macro foo(ex)
               ex
           end
       end
Main.A

julia> using .A: x, @foo

julia> x
1

julia> @foo 2 + 2
4

However, note that using and import statements still load the entire package. The part that you have control over is which names get added to your namespace.

Related