Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Julia, how do I display the contents of a macro?

Tags:

macros

julia

For example, I want to see what's "inside" of the @time macro. How would I do this?

like image 871
Badmanchild Avatar asked May 01 '14 08:05

Badmanchild


2 Answers

While it doesn't show you the macro itself, you can see the results of the macro expansion with macroexpand. For example:

julia> macroexpand(:(@time rand(10)))
:(begin  # util.jl, line 38:
        local #60#b0 = Base.gc_bytes() # line 39:
        local #61#t0 = Base.time_ns() # line 40:
        local #62#val = rand(10) # line 41:
        local #63#t1 = Base.time_ns() # line 42:
        local #64#b1 = Base.gc_bytes() # line 43:
        Base.println("elapsed time: ",Base./(Base.-(#63#t1,#61#t0),1.0e9)," seconds (",Base.-(#64#b1,#60#b0)," bytes allocated)") # line 44:
        #62#val
    end)

In this case, it also shows you where it is defined (util.jl, line 38), but that doesn't always happen. Since macros aren't first class objects themselves, the utilities such as which/edit/less (or their macro equivalents) don't work.

like image 69
mbauman Avatar answered Oct 12 '22 06:10

mbauman


I don't think there's a built in way to do this, but you can search the codebase for "macro X".

This sounds like it would be a useful feature, so unless someone corrects me and it already exists, you could always open an issue requesting it.

like image 44
one-more-minute Avatar answered Oct 12 '22 07:10

one-more-minute