‘show’ function for user-defined struct

julia> struct my
       age :: Int
       weight :: Float64
       end

julia> function Base.show(io::IO, h::my)
       print(io,"my struct")
       end

Here I defined a struct and a new method for the function ‘show’. Then when I instantiate a variable

julia> a = my(34,45.0)
my struct

the function ‘show’ is invoked automatically. I did not call it. What is the mechanism behind?

That’s just standard REPL behavior. When you evaluate an expression in REPL (without semicolon) the result is automatically printed on screen. The result is printed by REPL using the Base.show function automatically. See the doc here for more detail.

1 Like