# Proposed alias for union types

**URL:** <https://discourse.julialang.org/t/proposed-alias-for-union-types/108205>\
**Category:** Internals & Design\
**Tags:** question, proposal\
**Created:** [December 31, 2023, 1:43pm UTC](https://discourse.julialang.org/t/proposed-alias-for-union-types/108205 "2023-12-31T13:43:56Z")\
**Posts on this page:** 6\
**Page:** 8

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 17, 2024, 9:38am UTC](https://discourse.julialang.org/t/proposed-alias-for-union-types/108205/141 "2024-01-17T09:38:26Z")

</div>

> [@xiaoxi](#):
>
> Could [Cthulhu.jl](https://juliahub.com/ui/Packages/Cthulhu) and other similar tools use the parenthesis approach?

I think that these packages should stick to the standard Julia syntax (perhaps shortened with `…` when applicable for heavily parametric types), anything else should be confusing.

Also, with Cthulhu, complex union types are usually exactly what I am looking for, especially if they cause problems. I don’t see the point of making them less obtrusive.

---

<div class="post-metadata">

**Author:** ![MilesCranmer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/milescranmer/32/21070_2.png) [@MilesCranmer](https://discourse.julialang.org/u/MilesCranmer)\
**Post date:** [January 17, 2024, 10:54am UTC](https://discourse.julialang.org/t/proposed-alias-for-union-types/108205/142 "2024-01-17T10:54:08Z")

</div>

If it’s just for debugging one’s own library I think it could be helpful to have a configurable type alias system in Cthulhu’s outputs. Julia’s type system is the best I’ve encountered in any language, but it can admittedly get a bit verbose at times. Especially as some types stretch over the VSCode length limit in the Cthulhu labels and end up missing some information.

If the user could tweak union aliases in Cthulhu, it might also be useful to allow declaration of preferred abbreviations like `Float32` → `F32`, `Tuple` → `Tu`, etc, just to make the printouts less verbose. Again, this is just Cthulhu/JET/etc, this is **not** a suggestion for all of Julia, don’t worry 🙂

Maybe like

```julia
import Cthulhu: type_string

# default used in package:
# type_string(t) = string(t)

# user aliases:
type_string(::Type{Float32}) = "F32"
type_string(::Type{Union{A,B}}) where {A,B} = "(" * type_string(A) * "|" * type_string(B) * ")"
# etc.

```

which you could toss in your startup.jl

* * *

Aside: I’m also curious how, in a startup file, one could override printing globally for `Union{A,B}` → `(A|B)`? It’s obviously type piracy and subjective (and I wouldn’t advise for a package to do this) but if you are using infix unions in code anyways, it could be useful to also override printing locally. (Since you would presumably be able to copy-paste the outputs if you’ve already defined that alias on your system).

---

<div class="post-metadata">

**Author:** ![xiaoxi](https://avatars.discourse-cdn.com/v4/letter/x/a9adbd/32.png) [@xiaoxi](https://discourse.julialang.org/u/xiaoxi)\
**Post date:** [January 17, 2024, 1:05pm UTC](https://discourse.julialang.org/t/proposed-alias-for-union-types/108205/143 "2024-01-17T13:05:26Z")

</div>

I agree that Cthulhu.jl should use the standard Julia syntax by default, but could provide an option to display types using a less verbose syntax. It might be useful to test proposals that try to simplify the way types are communicated.

While I can understand that Cthulhu.jl and JET.jl deal with more pressing issues, I can imagine that more people would want a simplified syntax as those tools become more popular.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 17, 2024, 1:56pm UTC](https://discourse.julialang.org/t/proposed-alias-for-union-types/108205/144 "2024-01-17T13:56:34Z")

</div>

I imagine that the best place to start a discussion about this would be an issue at each package.

---

<div class="post-metadata">

**Author:** ![MilesCranmer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/milescranmer/32/21070_2.png) [@MilesCranmer](https://discourse.julialang.org/u/MilesCranmer)\
**Post date:** [January 17, 2024, 3:48pm UTC](https://discourse.julialang.org/t/proposed-alias-for-union-types/108205/145 "2024-01-17T15:48:18Z")

</div>

👍  
[Allow user to overload printing behavior · Issue #535 · JuliaDebug/Cthulhu.jl · GitHub](https://github.com/JuliaDebug/Cthulhu.jl/issues/535)

Edit: and now a PR here: [Create overloadable function for string representations of types by MilesCranmer · Pull Request #537 · JuliaDebug/Cthulhu.jl · GitHub](https://github.com/JuliaDebug/Cthulhu.jl/pull/537)

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [January 18, 2024, 1:29am UTC](https://discourse.julialang.org/t/proposed-alias-for-union-types/108205/146 "2024-01-18T01:29:12Z")

</div>

> [@MilesCranmer](#):
>
> Aside: I’m also curious how, in a startup file, one could override printing globally for `Union{A,B}` → `(A|B)`?

```julia-repl
julia> show(stdout, Union{Int8,UInt8})
Union{Int8, UInt8}

julia> @which show(stdout, Union{Int8,UInt8})
show(io::IO, x::Type)
     @ Base show.jl:953

```

> <https://github.com/JuliaLang/julia/blob/3120989f39bb7ef7863c4aab8ab1227cf71eec66/base/show.jl#L953>

That leads to `_show_type`.

> <https://github.com/JuliaLang/julia/blob/3120989f39bb7ef7863c4aab8ab1227cf71eec66/base/show.jl#L963-L970>

This suggests the following code.

```julia
julia> Base.show(io::IO, x::Union) = Base.show_delim_array(io, Base.uniontypes(x), '(', " |", ')', false)

julia> Union{Int8,UInt8}
(Int8 | UInt8)

julia> Union{Int8, UInt8, Int16, UInt16}
(Int16 | Int8 | UInt16 | UInt8)

julia> Base.show(io::IO, x::Union) = (Base.show_delim_array(io, Base.uniontypes(x), "|(", ',', ')', false); return)

julia> Union{Int8, UInt8, Int16, UInt16}
|(Int16, Int8, UInt16, UInt8)

```

[Previous page](https://discourse.julialang.org/t/proposed-alias-for-union-types/108205.md?page=7)
