Rust has the incredibly convenient dbg!
macro:
fn main() {
dbg!(1 + 2);
}
The above code outputs [src/main.rs:2] 1 + 2 = 3
.
Is there anything similar for Julia?
Or perhaps there's something else that serves my use case? I'd like to be able to output each one of my results, but removing the semicolon only displays the result for the last thing without a semicolon (instead of all things like MATLAB).
There are a number of macros built-in which do this:
julia> let x = 7
@show x^2
@info "cube of $x" x^3
@warn "minus" -x maxlog=3
end
x ^ 2 = 49
┌ Info: cube of 7
└ x ^ 3 = 343
┌ Warning: minus
│ -x = -7
└ @ Main REPL[42]:4
While the first just prints to stdout (see e.g. @macroexpand @show x
), macros @info
and @warn
use the Logging module, which has various more complex features, including suppressing some printout. @warn
and @error
print details of the call site, which (as @BallpointBen
mentions) can also be had directly from special macros @__MODULE__
, @__FILE__
and @__LINE__
.