Add support for optionally printing unsigned integers as decimals#60267
Add support for optionally printing unsigned integers as decimals#60267LilithHafner merged 3 commits intoJuliaLang:masterfrom
Conversation
|
For the decimal format I would recommend checking typeinfo and, if it matches the type, printing without any decoration We would need an extremely compelling use case to motivate introducing new syntax or pseudo-syntax/abbreviations like I bet you could implement this in a package with a display backend (or you could use piracy). |
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 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. |
|
(bump) |
|
From triage, Ideally, we would have a better way to configure REPL display options that doesn't require the options to be baked into Base. That said, there are not very many REPL display options folks have asked for and we don't think that there's going to be a new extensible REPL display option system any time soon (if you're reading this, please build one :) ), so it's fine to proceed with this approach now. |
LilithHafner
left a comment
There was a problem hiding this comment.
I'll merge in a day or two if no objections
|
I'm glad it passed triage, but to be clear this is an IOContext/ |
|
It would be better to implement this as a custom display backend, or to entirely overhaul the io API. The former being a smaller project. |
|
Oh, actually, this needs a news entry. Sorry I only realized that now. |
f498e86 to
338e886
Compare
|
No worries, added in 338e886. |
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 ausuffix, 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: