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

**URL:** https://discourse.julialang.org/t/how-to-stop-show-from-printing-a-types-fully-qualified-name/34274
**Category:** General Usage
**Tags:** question, type
**Created:** [February 6, 2020, 10:37pm UTC](https://discourse.julialang.org/t/how-to-stop-show-from-printing-a-types-fully-qualified-name/34274 "2020-02-06T22:37:39Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![NHDaly](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nhdaly/32/1413_2.png) [@NHDaly](https://discourse.julialang.org/u/NHDaly)
#### Post date: [February 6, 2020, 10:37pm UTC](https://discourse.julialang.org/t/how-to-stop-show-from-printing-a-types-fully-qualified-name/34274/1 "2020-02-06T22:37:39Z")

</div>

Say I have a type, `X`, and I overload its `show()`. When the type name comes up in other contexts, such as in an array, it still prints the fully qualified name:

```julia
julia> module M
           struct X
               x
           end
           
           Base.show(io::IO, x::X) = print(io, "X($(x.x))")
           
           println([X(1),X(2)])
       end
Main.M.X[X(1), X(2)]
Main.M

```

Is it bad form to overload `show(::IO, Type{X})` to stop doing that? Is there a way to do that only in certain contexts (such as, if it’s nested inside another object in the same module)?

```julia
julia> module M
           struct X
               x
           end
           
           # NOTE: Here I ONLY overload the Type printing, because the rest of the default behavior works great.
           Base.show(io::IO, x::Type{X}) = print(io, "X")
           
           println([X(1),X(2)])
       end
X[X(1), X(2)]
Main.M
```

---

<div class="post-metadata">

### Author: ![NHDaly](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nhdaly/32/1413_2.png) [@NHDaly](https://discourse.julialang.org/u/NHDaly)
#### Post date: [February 7, 2020, 4:46am UTC](https://discourse.julialang.org/t/how-to-stop-show-from-printing-a-types-fully-qualified-name/34274/2 "2020-02-07T04:46:27Z")

</div>

Asked on StackOverflow as well:  
[https://stackoverflow.com/q/60107220/751061](https://stackoverflow.com/q/60107220/751061)

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [February 7, 2020, 8:32am UTC](https://discourse.julialang.org/t/how-to-stop-show-from-printing-a-types-fully-qualified-name/34274/3 "2020-02-07T08:32:10Z")

</div>

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:

```julia
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](https://discourse.julialang.org/t/what-function-display-does-ijulia-call-at-the-end-of-each-cell/33554/2#)

---

<div class="post-metadata">

### Author: ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)
#### Post date: [February 7, 2020, 9:09am UTC](https://discourse.julialang.org/t/how-to-stop-show-from-printing-a-types-fully-qualified-name/34274/4 "2020-02-07T09:09:15Z")

</div>

AFAIK there is no context information of the type/module of the container (`:typeinfo` and `:module` are somewhat relevant). If you want to a brief output, I think the closest approximation may be to check `:compact` of `IOContext` and treat missing value as `true`:

```julia
module M

struct X
    x
end

function Base.show(io::IO, ::MIME"text/plain", x::X)
    if !get(io, :compact, true)
        print(io, "M.")
    end
    print(io, "X(")
    show(io, MIME"text/plain"(), x.x)
    print(io, ')')
end

end

```

then

```julia
julia> M.X(1)
X(1)

julia> [M.X(1), M.X(1)]
2-element Array{Main.M.X,1}:
 X(1)
 X(1)

```

> [@NHDaly](#):
>
> Is it bad form to overload `show(::IO, Type{X})` to stop doing that?

> [@JeffreySarnoff](#):
>
> `Base.show(io::IO, x::MyString) = print(io, x.s)`

I think it’s better to avoid touching `show(::IO, ::T)`. It is called via `repr` and it is better if the output of `repr` is a valid Julia code.

Unfortunately, Julia before 1.5 calls 2-arg `show` via 3-arg `show` of containers so above method is not perfect, though.
