Julia: Output of show(bitvector) is misleading

Created on 10 Sep 2018  Â·  9Comments  Â·  Source: JuliaLang/julia

julia> z = falses(3)
3-element BitArray{1}:
 false
 false
 false

julia> show(z)
Bool[false, false, false]

I sort of understand this, but it's misleading because it gives the impression that the bitvector is a Vector{Bool}.

arrays display and printing

Most helpful comment

@KristofferC sure, but the question is about show. Sure, that falls back to print, but the point of show is:

The representation used by show generally includes Julia-specific formatting and type information.

All 9 comments

We simply don't have a very nice BitArray array literal — the best we got is BitArray([false, false, false]), but that still allocates an intermediate boolean array. We could add a BitArray literal with the new @BitArray[false, false, false] macro syntax, but the default printing of custom arrays will still show up looking like normal arrays.

We could use the pseudo-syntax:

Bool[false, false, false]::BitArray{1}

Which of course is an error as Julia syntax, but at least it flags the fact that it's not an Array.

Yep, I get it. The big problem I have with this is that it's impossible to distinguish between a bitvec and a vector of booleans when you're printing them. Anything that would clarify that "this is a bitvector as opposed to a vector of booleans" would be useful, IMO.

Yep, I get it. The big problem I have with this is that it's impossible to distinguish between a bitvec and a vector of booleans when you're printing them.

This is not in the contract for print, in fact it is explicitly the opposite.

The representation used by print includes minimal formatting and tries to avoid Julia-specific details.

julia> print(3.0)
3.0
julia> print(Float32(3.0))
3.0

This is the same with other arrays, e.g.

julia> using StaticArrays
julia> a=@SVector Float32[1,2,3];
julia> show(a)
Float32[1.0, 2.0, 3.0]

That is, show is generally not faithfully showing type information of containers.

However, for both BitVector and Vector{Bool} a display based on bitstring is much nicer (in almost every situation). Likewise Vector{UInt8} could have a much nicer printing (like any hex editor).

@KristofferC sure, but the question is about show. Sure, that falls back to print, but the point of show is:

The representation used by show generally includes Julia-specific formatting and type information.

(@mbauman - I was literally pasting the exact same thing when you beat me to it.)

I agree it would be good to have a standard way to show different types of arrays that preserves their types. @BitArray[true, false] is one candidate. See also #28905 --- a more thorough redesign of array showing may be in order.

I also notice we could avoid printing Bool in Bool[true, false].

It's not an issue if the printed syntax would allocate a copy. People are unlikely to copy it in their code anyway. What matter is that the type is visible. So BitArray([true, false]) sounds like the best option.

The one downside to BitArray([true, false]) is that it doesn't generalize to other array types — we don't have default constructors/converters to make that meaningful for all AbstractArrays. E.g.:

julia> @show view([true, false, true], :);
view([true, false, true], :) = Bool[true, false, true]

julia> SubArray([true, false, true])
ERROR: MethodError: Cannot `convert` an object of type Array{Bool,1} to an object of type SubArray

Maybe that's just fine, though. My type-assert idea would always be an error, and this would indeed often work.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wilburtownsend picture wilburtownsend  Â·  3Comments

iamed2 picture iamed2  Â·  3Comments

musm picture musm  Â·  3Comments

tkoolen picture tkoolen  Â·  3Comments

m-j-w picture m-j-w  Â·  3Comments