Skip to content

Conversation

@LilithHafner
Copy link
Member

@LilithHafner LilithHafner commented Feb 28, 2023

Fixes #29109 by changing the generic implementation. See a critique of this proposal by @mbauman here.

Before

julia> show(BitArray(rand(Bool, 3)))
Bool[0, 0, 1]
julia> show(BitArray(rand(Bool, 3,3)))
Bool[1 1 1; 1 0 1; 1 1 0]
julia> show(Diagonal(rand(3)))
[0.6797235523390357 0.0 0.0; 0.0 0.48169476804551536 0.0; 0.0 0.0 0.7412670944167903]
julia> show(view([1,2,3], :))
[1, 2, 3]

After (out of date)

julia> show(BitArray(rand(Bool, 3)))
BitVector([1, 1, 1])
julia> show(BitArray(rand(Bool, 3,3)))
BitMatrix([0 1 0; 0 0 0; 1 1 1])
julia> show(Diagonal(rand(3)))
Diagonal{Float64, Vector{Float64}}([0.35780770561720376 0.0 0.0; 0.0 0.42381095992478834 0.0; 0.0 0.0 0.5950554958506619])
julia> show(view([1,2,3], :))
SubArray{Int64, 1, Vector{Int64}, Tuple{Base.Slice{Base.OneTo{Int64}}}, true}([1, 2, 3])

This is not a clearcut win because the final two examples parse but evaluate to error. This changes evaluating to the wrong container type to an error.

This PR has no impact for arrays with dimension >=3 because of #48828

@LilithHafner LilithHafner added speculative Whether the change will be implemented is speculative arrays [a, r, r, a, y, s] display and printing Aesthetics and correctness of printed representations of objects. labels Feb 28, 2023
@jishnub
Copy link
Member

jishnub commented Mar 1, 2023

This seems way too verbose, and potentially exposes implementation detail such as Base.Slice to the user. I wonder if there's a more concise format that contains the nameof, but not the type parameters? The parameters often correspond to fields that users shouldn't care about, or access directly.

@LilithHafner
Copy link
Member Author

Agreed. How's this, @jishnub?

julia> show(BitArray(rand(Bool, 3)))
BitVector([1, 0, 1])
julia> show(BitArray(rand(Bool, 3,3)))
BitMatrix([1 1 0; 0 1 1; 1 1 1])
julia> show(Diagonal(rand(3)))
Diagonal([0.6230741840882028 0.0 0.0; 0.0 0.27300627395901567 0.0; 0.0 0.0 0.10329253077665224])
julia> show(view([1,2,3], :))
SubArray([1, 2, 3])
julia> show(Diagonal(rand(Float16, 3)))
Diagonal(Float16[0.1245 0.0 0.0; 0.0 0.3525 0.0; 0.0 0.0 0.004883])

I changed it to display the container typealias if it has one and otherwise omit all type parameters. If the eltype cannot be inferred from the printed container type, nor can it be inferred from the elements themselves, then it is included with the array.

@timholy
Copy link
Member

timholy commented Jun 28, 2023

Note that showarg-type display would be nice for both show(Diagonal(rand(3))) and show(view([1,2,3], :)), where the call to create them is notably more compact than the generated display.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arrays [a, r, r, a, y, s] display and printing Aesthetics and correctness of printed representations of objects. speculative Whether the change will be implemented is speculative

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Output of show(bitvector) is misleading

4 participants