Why are booleans printed as numbers?

With Julia 1.10.4:

julia> [("a", true), ("b", false)]
2-element Vector{Tuple{String, Bool}}:
 ("a", 1)
 ("b", 0)

Here true / false are confusingly printed as 1 / 0.

4 Likes

The behavior seems to depend on exactly what you print:

julia> [["a", true], ["b", false]]
2-element Vector{Vector{Any}}:
 ["a", true]
 ["b", false]

julia> [("a", true), ("b", false)]
2-element Vector{Tuple{String, Bool}}:
 ("a", 1)
 ("b", 0)

Arrays of tuples trigger 0/1 printing, but arrays of Vector{Any} don’t trigger this. I guess the bool type needs to appear explicitly to trigger this behavior.

Yes, it looks at the :typeinfo parameter of the IOContext β€” if the type Bool has already been displayed (e.g. as part of the array type), then it prints the compact numerical value.

1 Like

In Julia, Bool values are a type of number, and true and false are equal to 1 and 0, respectively:

julia> Bool <: Integer
true

julia> true == 1
true

julia> false == 0
true
5 Likes