DbgMacro.jl
Documentation for DbgMacro.jl.
This package provides four macros: @dbg
, @dumpct
, @dumprt
and @qn
.
When executing
@dbg ex1 ex2 ex3 ...
the macro generates code that displays all the expressions in the same way as @show
does, each on a separate line, preceded by the location in the format module:file:line
. The output goes to stderr
. Useful for debugging.
It is inspired by Rust dbg!
macro.
The macros @dumpct
and @dumprt
@dumpct expression
@dumprt expression
dump the provided expression at compile-time or run-time respectively.
The macro @qn
@qn expression
returns the quoted expression without interpolating contained $
.
Examples
julia> using DbgMacro
julia> function fact(n::Integer) @dbg n n < 0 && error("`n` must be positive") n == 0 && return 1 n * fact(n-1) end
fact (generic function with 1 method)
julia> fact(5)
Main:REPL[2]:2 n = 5 Main:REPL[2]:2 n = 4 Main:REPL[2]:2 n = 3 Main:REPL[2]:2 n = 2 Main:REPL[2]:2 n = 1 Main:REPL[2]:2 n = 0 120
julia> function ct(x) @dumpct :x + x + $x x end
Expr head: Symbol call args: Array{Any}((4,)) 1: Symbol + 2: QuoteNode value: Symbol x 3: Symbol x 4: Expr head: Symbol $ args: Array{Any}((1,)) 1: Symbol x ct (generic function with 1 method)
julia> ct(42)
42
julia> function rt(x) @dumprt :x + x + $x x end
rt (generic function with 1 method)
julia> rt(42)
Expr head: Symbol call args: Array{Any}((4,)) 1: Symbol + 2: QuoteNode value: Symbol x 3: Symbol x 4: Expr head: Symbol $ args: Array{Any}((1,)) 1: Symbol x 42
julia> @qn :x + x + $x
:(:x + x + $(Expr(:$, :x)))
Index
Library
DbgMacro.@dbg
— Macro@dbg <expr1> <expr2> <expr3> ...
Display all the expressions in the same way as @show
does, each on a separate line, preceded by the location in the format module:file:line
.
The output goes to stderr
.
Useful for debugging.
Examples
julia> m = [1 2; 3 4];
julia> @dbg 1+2 "Hello" m
Main:none:1 1 + 2 = 3
Main:none:1 "Hello" = "Hello"
Main:none:1 m = [1 2; 3 4]
DbgMacro.@dumpct
— Macro@dumpct <expression>
Dump the expression at compile-time.
Examples
julia> function foo(x)
@dumpct :x + x + $x
x
end
Expr
head: Symbol call
args: Array{Any}((4,))
1: Symbol +
2: QuoteNode
value: Symbol x
3: Symbol x
4: Expr
head: Symbol $
args: Array{Any}((1,))
1: Symbol x
foo (generic function with 1 method)
julia> foo(42)
42
DbgMacro.@dumprt
— Macro@dumprt <expression>
Dump the expression at run-time.
Examples
julia> function foo(x)
@dumprt :x + x + $x
x
end
foo (generic function with 1 method)
julia> foo(42)
Expr
head: Symbol call
args: Array{Any}((4,))
1: Symbol +
2: QuoteNode
value: Symbol x
3: Symbol x
4: Expr
head: Symbol $
args: Array{Any}((1,))
1: Symbol x
42
DbgMacro.@qn
— Macro@qn <expression>
Return the expression, without $ interpolation.
Examples
julia> @qn foo(1, x, :y, $z)
:(foo(1, x, :y, $(Expr(:$, :z))))