Display NTuples with more than one element as NTuples rather than Tuples?

This example is somewhat specific to making displayed types more legible, although this might help more generally.
Currently, we have

julia> Tuple{Int, Int, Int}
Tuple{Int64, Int64, Int64}

julia> Tuple{Int, Int, Int, Int}
NTuple{4, Int64}

I feel the latter form is much more readable, and would prefer this display for 2 or 3-element NTuples as well. This becomes especially important if the individual elements are long:

julia> using BlockArrays, ArrayLayouts

julia> T = BlockedUnitRange{ArrayLayouts.RangeCumsum{Int64, StepRange{Int64, Int64}}};

julia> Tuple{T, T, T}
Tuple{BlockedUnitRange{RangeCumsum{Int64, StepRange{Int64, Int64}}}, BlockedUnitRange{RangeCumsum{Int64, StepRange{Int64, Int64}}}, BlockedUnitRange{RangeCumsum{Int64, StepRange{Int64, Int64}}}}

julia> Tuple{T, T, T, T}
NTuple{4, BlockedUnitRange{RangeCumsum{Int64, StepRange{Int64, Int64}}}}

The latter is certainly more concise and easier to parse than the former.

10 Likes

PR created:

2 Likes

Would this constitute a breaking change? I should hope that nobody is using string(typeof(some_tuple))) in Julia code but it might matter to some shell script.

1 Like

There have been quite a few changes to displayed forms in minor releases, e.g. named tuples in a recent release, and the Array{T,1} to Vector{T} change sometime bank. This isn’t seen as breaking.

This is really a taste/preference thing. Personally, I like the current print-up-to-three behavior; I often find three is used as a cutoff before eliding in many contexts.

If you just have a handful of commonly-used long types like that, you could export them from your package and then printing will use those shorthands:

julia> import BlockArrays, ArrayLayouts

julia> @eval BlockArrays begin
       const StepAxis = BlockedUnitRange{ArrayLayouts.RangeCumsum{Int64, StepRange{Int64, Int64}}}
       export StepAxis
       end

julia> T = BlockArrays.BlockedUnitRange{ArrayLayouts.RangeCumsum{Int64, StepRange{Int64, Int64}}}
StepAxis (alias for BlockArrays.BlockedUnitRange{ArrayLayouts.RangeCumsum{Int64, StepRange{Int64, Int64}}})

julia> Tuple{T,T,T}
Tuple{BlockArrays.StepAxis, BlockArrays.StepAxis, BlockArrays.StepAxis}
1 Like