# Error with \`show\` for abbreviated type printing

**URL:** https://discourse.julialang.org/t/error-with-show-for-abbreviated-type-printing/62190
**Category:** General Usage
**Created:** [June 1, 2021, 1:41pm UTC](https://discourse.julialang.org/t/error-with-show-for-abbreviated-type-printing/62190 "2021-06-01T13:41:19Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)
#### Post date: [June 1, 2021, 1:41pm UTC](https://discourse.julialang.org/t/error-with-show-for-abbreviated-type-printing/62190/1 "2021-06-01T13:41:19Z")

</div>

Let’s say I have a type with an abbreviated `show` method for the type itself (for shortened stacktraces).

```julia
struct Blarg{X,Y}
    x::X
    y::Y
end

Base.show(io::IO, ::Type{Blarg{X,Y}}) where {X,Y} = print(io, "Blarg{$X,...}")
Base.show(io::IO, ::MIME"text/plain", ::Type{Blarg{X,Y}}) where {X,Y} = print(io, "Blarg{$X,$Y}")

```

Now if I define a function that uses type parameter annotations, I get an error. What is happening here? Why does it say `X not defined`?

```julia
julia> f(blarg::Blarg{X,Y}) where {X,Y} = blarg.x + blarg.y
f (generic function with 1 method)

julia> methods(f)
# 1 method for generic function "f":
[1] Error showing value of type Base.MethodList:
ERROR: UndefVarError: X not defined
Stacktrace:
  [1] show(io::IOContext{IOBuffer}, #unused#::
SYSTEM (REPL): showing an error caused an error
ERROR: UndefVarError: X not defined
Stacktrace:
  [1] show(io::IOContext{IOBuffer}, #unused#::
SYSTEM (REPL): caught exception of type UndefVarError while trying to handle a nested exception; giving up

```

Note that if the function definition doesn’t use type parameter annotations, there isn’t a problem:

```julia
julia> new_f(blarg::Blarg) = blarg.x
new_f (generic function with 1 method)

julia> methods(new_f)
# 1 method for generic function "new_f":
[1] new_f(blarg::Blarg) in Main at REPL[6]:1

```

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [June 1, 2021, 1:53pm UTC](https://discourse.julialang.org/t/error-with-show-for-abbreviated-type-printing/62190/2 "2021-06-01T13:53:11Z")

</div>

> [@jonniedie](#):
>
> `::Type{Blarg{X,Y}}`

use

```julia
::Blarg{X,Y}

```

instead of `Type{Blarg{X,Y}}` and it works (I don’t know exactly what that `Type` could be doing there, but I didn’t use it for a similar function before).

---

<div class="post-metadata">

### Author: ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)
#### Post date: [June 1, 2021, 1:55pm UTC](https://discourse.julialang.org/t/error-with-show-for-abbreviated-type-printing/62190/3 "2021-06-01T13:55:14Z")

</div>

The `Type` is important here. I’m not trying to control the printing for the instance of the type, I’m trying to control the printing for the type itself when it’s inside of stacktraces.

---

<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: [June 2, 2021, 8:56am UTC](https://discourse.julialang.org/t/error-with-show-for-abbreviated-type-printing/62190/4 "2021-06-02T08:56:17Z")

</div>

It is generally recommended not to overload `show` for Types. E.g. [https://github.com/JuliaLang/julia/issues/22363#issuecomment-799984810](https://github.com/JuliaLang/julia/issues/22363#issuecomment-799984810) and [These stack traces are out of control - #5 by cstjean](https://discourse.julialang.org/t/these-stack-traces-are-out-of-control/47162/5)  
There has been discussion about how to handle this though (such as the discourse thread above). For now, there doesn’t seem to be a satisfying solution.

---

<div class="post-metadata">

### Author: ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)
#### Post date: [June 2, 2021, 9:01am UTC](https://discourse.julialang.org/t/error-with-show-for-abbreviated-type-printing/62190/5 "2021-06-02T09:01:24Z")

</div>

I think this is probably [Dispatching on the result of unwrap\_unionall seems weird?](https://discourse.julialang.org/t/dispatching-on-the-result-of-unwrap-unionall-seems-weird/25677) and the answer is you want to use the new type aliasing stuff in Julia 1.6 rather than custom overloaded `show`.

---

<div class="post-metadata">

### Author: ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)
#### Post date: [June 2, 2021, 1:24pm UTC](https://discourse.julialang.org/t/error-with-show-for-abbreviated-type-printing/62190/6 "2021-06-02T13:24:47Z")

</div>

@tomerarnon Ah, that’s unfortunate. The combination of DifferentialEquations.jl’s type parameterizations and ComponentArrays.jl’s value-as-type axis types make stack traces pretty useless without some type of abridged printing.

@marius311 Thanks for the tip on the aliasing solution, I’ll have to check that out. Hopefully it offers some distinction between MIME types because it’s important to be able to see a fully-printed type when calling `typeof` directly. This wouldn’t be a strict aliasing situation.

---

<div class="post-metadata">

### Author: ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)
#### Post date: [June 2, 2021, 9:09pm UTC](https://discourse.julialang.org/t/error-with-show-for-abbreviated-type-printing/62190/7 "2021-06-02T21:09:46Z")

</div>

Never played with it so don’t know if it does, but if it doesn’t, another option is to use `@isdefined` in your custom `show` to avoid errors. You’ll have to play with in what scenarios things come in as defined or not, its a bit of a mystery to me still.

---

<div class="post-metadata">

### Author: ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)
#### Post date: [June 2, 2021, 9:58pm UTC](https://discourse.julialang.org/t/error-with-show-for-abbreviated-type-printing/62190/8 "2021-06-02T21:58:58Z")

</div>

You can overload the internal function which is used by stacktrace printing. No guarantee this will keep working, but it ought not to change anything else, like `methods(f)`, at the moment:

```julia
julia> function Base.print_type_stacktrace(io, type; color=:normal)
           str = first(sprint(show, type, context=io), 20) # <-- only change, could add ... etc.
           i = findfirst('{', str)
           if isnothing(i) || !get(io, :backtrace, false)::Bool
               printstyled(io, str; color=color)
           else
               printstyled(io, str[1:prevind(str,i)]; color=color)
               printstyled(io, str[i:end]; color=:light_black)
           end
       end

julia> map(+, (((((1,),),),),))
ERROR: MethodError: ... 
Stacktrace:
 [1] map(f::typeof(+), t::Tuple{Tuple{Tuple{Tu) # <-- trimmed here
   @ Base ./tuple.jl:213

```

---

<div class="post-metadata">

### Author: ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)
#### Post date: [June 2, 2021, 11:05pm UTC](https://discourse.julialang.org/t/error-with-show-for-abbreviated-type-printing/62190/9 "2021-06-02T23:05:09Z")

</div>

That looks like the way to go. Thank you!
