# Weird Base.show behavior with custom Struct

**URL:** <https://discourse.julialang.org/t/weird-base-show-behavior-with-custom-struct/91321>\
**Category:** General Usage\
**Tags:** question, show, graphs\
**Created:** [December 6, 2022, 8:05pm UTC](https://discourse.julialang.org/t/weird-base-show-behavior-with-custom-struct/91321 "2022-12-06T20:05:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RobertGregg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robertgregg/32/22105_2.png) [@RobertGregg](https://discourse.julialang.org/u/RobertGregg)\
**Post date:** [December 6, 2022, 8:05pm UTC](https://discourse.julialang.org/t/weird-base-show-behavior-with-custom-struct/91321/1 "2022-12-06T20:05:55Z")

</div>

For some reason checking my custom Struct in the REPL differs from the Base.show() method I defined. Can anyone replicate and/or explain why this might be happening?

Here is a MWE to (hopefully) replicate the behavior:

```julia
using Graphs
import Base: show

mutable struct myNewGraph{N, T<:Integer} <: Graphs.AbstractSimpleGraph{T}
    ne::N #Number of edges
    fadjlist::Vector{Vector{T}} #Sorted adjacency list [src]: (dst, dst, dst)
end

function show(io::IO, space::myNewGraph)
    println(io, "My new type with $(space.ne) edge")
end

Graphs.is_directed(::Type{<:myNewGraph}) = false
Graphs.is_directed(g::myNewGraph) = false

space = myNewGraph(1, [[1]])

```

And here is what happens when I display the struct instance in the REPL:

```julia
julia> space
{1, 1} undirected simple Int64 graph

julia> show(space)
My new type with 1 edge

julia> Base.show(space)
My new type with 1 edge

```

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [December 6, 2022, 8:10pm UTC](https://discourse.julialang.org/t/weird-base-show-behavior-with-custom-struct/91321/2 "2022-12-06T20:10:21Z")

</div>

Yeah, this is a bit confusing, the explanation for it is buried in here: [Types · The Julia Language](https://docs.julialang.org/en/v1/manual/types/#man-custom-pretty-printing)

Basically, when you display a top level form in the REPL, what get’s called is not `show(stdout, x)` but instead `show(stdout, MIME"text/plain"(), x)`. So the easiest fix for you would be to write something like

```julia
Base.show(io::IO, space::myNewGraph) = println(io, "My new type with $(space.ne) edge")
Base.show(io::IO, ::MIME"text/plain", space::myNewGraph) = show(io, space)

```

---

<div class="post-metadata">

**Author:** ![RobertGregg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robertgregg/32/22105_2.png) [@RobertGregg](https://discourse.julialang.org/u/RobertGregg)\
**Post date:** [December 6, 2022, 8:20pm UTC](https://discourse.julialang.org/t/weird-base-show-behavior-with-custom-struct/91321/3 "2022-12-06T20:20:24Z")

</div>

I’ll definitely have to read this part of the manual more carefully. Do you know if this is a recent change in the Base.show() behavior? I was revising some code from a few months ago when I stumbled upon this. (also thank you, I was banging my head against the wall trying to figure this out)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 7, 2022, 12:16am UTC](https://discourse.julialang.org/t/weird-base-show-behavior-with-custom-struct/91321/4 "2022-12-07T00:16:58Z")

</div>

> [@RobertGregg](#):
>
> Do you know if this is a recent change in the Base.show() behavior?

It’s been this way since before Julia 1.0

However, realize that the 3-argument `show(io, mimetype, x)` calls the 2-argument `show(io, x)` by default, so when you define a new type you normally only need to define the latter if you want the two cases to look the same.

The problem here is that, 6 months ago, Graphs.jl decided to override `show(io, ::MIME"text/plain", x)` instead of `show(io, x)` ([Graphs.jl#121](https://github.com/JuliaGraphs/Graphs.jl/pull/121)), so whenever you define a subtype of `Graphs.AbstractSimpleGraph` you need to override _both_ `show` methods if you want customized behavior in both the REPL and in `print` etcetera.
