# Show an object in extended form vs short form

**URL:** <https://discourse.julialang.org/t/show-an-object-in-extended-form-vs-short-form/48792>\
**Category:** General Usage\
**Tags:** display, show\
**Created:** [October 22, 2020, 3:21am UTC](https://discourse.julialang.org/t/show-an-object-in-extended-form-vs-short-form/48792 "2020-10-22T03:21:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [October 22, 2020, 3:21am UTC](https://discourse.julialang.org/t/show-an-object-in-extended-form-vs-short-form/48792/1 "2020-10-22T03:21:09Z")

</div>

my problem is the following:  
I have a type with fields, and i want those fields to be displayed with additional detail when shown alone in the REPL:

```julia
julia> a = MyType(;params....)
MyType properties, with:
a: ....
b: .....

```

Also, i want this type to be displayed shortly when inside of a container:

```julia
julia> a = MyType(;params....);b = MyType(;params2....);(a,b) #tuple, it can be a vector
(MyType(a=1,b=2...),MyType(a2=2,b2=2))

```

i know that arrays implement this type of contex-aware `show`, as:

```julia
julia> a = rand(2)
2-element Array{Float64,1}:
 0.3972476935276372
 0.25401198033393935

julia> (a,a)
([0.3972476935276372, 0.25401198033393935], [0.3972476935276372, 0.25401198033393935])

```

any ideas on how to implement this, What functions do i have to overload to change the behaviour?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [October 22, 2020, 3:43am UTC](https://discourse.julialang.org/t/show-an-object-in-extended-form-vs-short-form/48792/2 "2020-10-22T03:43:58Z")

</div>

[https://docs.julialang.org/en/v1/base/io-network/#Base.show-Tuple{IO,Any,Any}](https://docs.julialang.org/en/v1/base/io-network/#Base.show-Tuple%7BIO,Any,Any%7D)  
Short answer is you over-ride `Base.show` with a behavior for regular and compact contexts

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [October 22, 2020, 3:52am UTC](https://discourse.julialang.org/t/show-an-object-in-extended-form-vs-short-form/48792/3 "2020-10-22T03:52:49Z")

</div>

> [@Oscar\_Smith](#):
>
> Short answer is you over-ride `Base.show` with a behavior for regular and compact contexts

i have this code to test the behaviour, but the IO doesnt pass a `compact = true` when inside arrays or tuples:

```julia
struct MyType
    a
end

function Base.show(io::IO, x::MyType)
    compact = get(io,:compact,false)
    a = x.a
    if compact
        println("My type with one element:")
        println("a (",typeof(a),"):",a)
    else
        print("MyType(",a,")")
    end
end

a = MyType(2)
b = (a,a)

```

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [October 22, 2020, 3:58am UTC](https://discourse.julialang.org/t/show-an-object-in-extended-form-vs-short-form/48792/4 "2020-10-22T03:58:54Z")

</div>

to add more information:

```julia
julia> show(rand(2))
[0.24607512874238346, 0.7404663542415493]
julia> display(rand(2))
2-element Array{Float64,1}:
 0.3096292761559558
 0.7676667676433597

```

i know that the `show` method ultimately dispatches to `display`

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [October 22, 2020, 9:38am UTC](https://discourse.julialang.org/t/show-an-object-in-extended-form-vs-short-form/48792/5 "2020-10-22T09:38:25Z")

</div>

Rather than with `compact`, the way to do this is to overload two methods of `show`. One with signature `show(::IO, ::MyObject)`, which will apply for “compact printing” and another with signature `show(::IO, ::MIME"text/plain", ::MyObject)` which will apply to “display” printing like top level repl.

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [October 23, 2020, 7:49pm UTC](https://discourse.julialang.org/t/show-an-object-in-extended-form-vs-short-form/48792/6 "2020-10-23T19:49:30Z")

</div>

> [@tomerarnon](#):
>
> `show(::IO, ::MIME"text/plain", ::MyObject)`

yes! this is what i’m looking for: a proof of concept:

```julia
struct MyType
    a
end
function Base.show(io::IO, x::MyType)
    a = x.a    
    print(io,"MyType(",a,")")
    end

function Base.show(::IO, ::MIME"text/plain", x::MyType)
    a = x.a
    println(io,"My type with one element:")
    println(io,"a (",typeof(a),"):",a)
end

```

in the REPL:

```julia
julia> a = MyType(2)
My type with one element:
a (Int64):2

julia> (a,a)
(MyType(2), MyType(2))

```
