Combining @show with display

Hi,

I have been looking for a way to output values with their variable name. @show does exactly this, except the output is not as pretty as when using display. Is there a way I can combine both of them and have something like @out var with an output similar to

var = 
[display output]

Thanks.

This has certainly been proposed. See #15820 Define macro @display and the discussion thread How to print arrays inside functions? - #5 by antoine-levitt … it wouldn’t be crazy to add something like this, but no one has done it yet.

See also #22253 Use multi-line format in @show which at one point changed @show to use a display-like format, but this was subsequently reverted in #25849 because it broke the repr property that you could cut-and-paste @show results into code.

You could always define your own macro based on the code in #22253, of course:

macro myshow(exs...)
    blk = Expr(:block)
    for ex in exs
        push!(blk.args, :(print($(sprint(Base.show_unquoted,ex)*" = "))))
        push!(blk.args, :(show(stdout, "text/plain", begin value=$(esc(ex)) end)))
        push!(blk.args, :(println()))
    end
    isempty(exs) || push!(blk.args, :value)
    return blk
end

which gives

julia> @show rand(5);
rand(5) = [0.15242788991869327, 0.822593384182898, 0.24466669272136954, 0.9124861334431887, 0.6474094076037362]

julia> @myshow rand(5);
rand(5) = 5-element Vector{Float64}:
 0.2430428495428918
 0.556988929839062
 0.9504355130227148
 0.07313670570961417
 0.7135984118032906
2 Likes

Thanks for the insight and the code, works as expected :slight_smile: