Skip to content

Commit

Permalink
Add macro version of dump (JuliaLang#22980)
Browse files Browse the repository at this point in the history
* Add macro version of dump

* Update dump macro to show original expression

* Rename to dumpexpr

* Moved into Meta module

* Avoid 32-bit issue
  • Loading branch information
omus committed Aug 1, 2017
1 parent 18da73e commit 2653063
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
13 changes: 12 additions & 1 deletion base/meta.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ module Meta

export quot,
isexpr,
show_sexpr
show_sexpr,
@dump

quot(ex) = Expr(:quote, ex)

Expand Down Expand Up @@ -45,4 +46,14 @@ function show_sexpr(io::IO, ex::Expr, indent::Int)
end
end

"""
@dump expr
Show every part of the representation of the given expression. Equivalent to
`dump(:(expr))`.
"""
macro dump(expr)
dump(expr)
end

end # module
2 changes: 1 addition & 1 deletion doc/src/manual/metaprogramming.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ Expr
```

(to view the structure of this expression, try `ex.head` and `ex.args`, or use [`dump()`](@ref)
as above)
as above or [`Meta.@dump`](@ref))

Note that equivalent expressions may be constructed using [`parse()`](@ref) or the direct `Expr`
form:
Expand Down
1 change: 1 addition & 0 deletions doc/src/stdlib/io-network.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Base.Printf.@sprintf
Base.sprint
Base.showerror
Base.dump
Meta.@dump
Base.readline
Base.readuntil
Base.readlines
Expand Down
26 changes: 26 additions & 0 deletions test/meta.jl
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,29 @@ end
@test collect(methods(_nospec_with_default))[2].nospecialize == 1
@test _nospec_with_default() == 2
@test _nospec_with_default(10) == 20


let oldout = STDOUT
local rdout, wrout, out
try
rdout, wrout = redirect_stdout()
out = @async read(rdout, String)

@test eval(:(@dump x + y)) === nothing

redirect_stdout(oldout)
close(wrout)

@test wait(out) == """
Expr
head: Symbol call
args: Array{Any}((3,))
1: Symbol +
2: Symbol x
3: Symbol y
typ: Any
"""
finally
redirect_stdout(oldout)
end
end

0 comments on commit 2653063

Please sign in to comment.