Skip to content

Conversation

@JamesWrigley
Copy link
Member

@JamesWrigley JamesWrigley commented Nov 27, 2025

This is something I've wanted for a while as I sometimes work with unsigned data. There's some previous discussion here: #30167. Note that this PR only adds an option to enable decimal printing rather than making it the default.

Following a suggestion in that issue I made show() add a u suffix, but one could argue people would mistake it as Julia syntax for unsigned literals (as it is in e.g. C++). I think that's an ok tradeoff to keep it obvious that a value is unsigned, but I don't feel very strongly about it.

Some examples of it in action:

julia> function Base.show(io::IO, n::Unsigned)
           if get(io, :hexunsigned, true)::Bool
               print(io, "0x", string(n, pad = sizeof(n)<<1, base = 16))
           else
               print(io, string(n), "u")
           end
       end

julia> Base.active_repl.options.iocontext[:hexunsigned] = false
false

julia> rand(UInt8, 10)
10-element Vector{UInt8}:
 222u
 249u
  72u
 143u
 136u
  25u
 103u
  55u
  25u
  22u

julia> using Sockets

julia> listenany(8080)
(8080u, Sockets.TCPServer(RawFD(23) active))

@inkydragon inkydragon added the display and printing Aesthetics and correctness of printed representations of objects. label Nov 28, 2025
@LilithHafner
Copy link
Member

For the decimal format I would recommend checking typeinfo and, if it matches the type, printing without any decoration 123. If it does not match (including unset typeinfo) then print UInt64(123).

We would need an extremely compelling use case to motivate introducing new syntax or pseudo-syntax/abbreviations like 123u.

I bet you could implement this in a package with a display backend (or you could use piracy).

@JamesWrigley
Copy link
Member Author

For the decimal format I would recommend checking typeinfo and, if it matches the type, printing without any decoration 123. If it does not match (including unset typeinfo) then print UInt64(123).

Sure, I think that's even better; changed in f498e86. New examples:

julia> function Base.show(io::IO, n::Unsigned)
           if get(io, :hexunsigned, true)::Bool
               print(io, "0x", string(n, pad = sizeof(n)<<1, base = 16))
           else
               if get(io, :typeinfo, Nothing)::Type == typeof(n)
                   print(io, n)
               else
                   print(io, typeof(n), "($n)")
               end
           end
       end

julia> Base.active_repl.options.iocontext[:hexunsigned] = false
false

julia> rand(UInt8, 10)
10-element Vector{UInt8}:
  24
 246
  41
  59
 227
 230
 232
  35
 229
 219

julia> using Sockets

julia> listenany(8080)
(UInt16(8080), Sockets.TCPServer(RawFD(23) active))

I bet you could implement this in a package with a display backend (or you could use piracy).

I actually did do it by type-piracy in my startup.jl for like a year, but the extra latency from invalidations was too much in the end so I recently disabled it (hence this PR). I'm not sure how it could be done with a custom display, but in any case I'd argue this is basic (and wanted) enough to be in Base.

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

Labels

display and printing Aesthetics and correctness of printed representations of objects.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants