How to stop `show` from printing a type's fully qualified name?

When making a custom type, like a struct , the author of the custom type often provides a version of Base.show specialized to that type. Here is a good pattern to follow:

struct MyString
    s::String
end

mystring = MyString("my string") # shows: MyString("my string")

# this is used to handle a call to `print`
Base.show(io::IO, x::MyString) = print(io, x.s)
# this is used to show values in the REPL and when using IJulia
Base.show(io::IO, m::MIME"text/plain", x::MyString) = print(io, x.s)

mystring = MyString("my string") # shows: my string
print(mystring)                  # shows: "my string" 
mystring = MyString("my string") # shows: MyString("my string")

this information comes from a Discourse entry written by Steven G. Johnson

3 Likes