Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keywords for string, deprecate bin, oct, dec, hex, base #25717

Closed
wants to merge 11 commits into from
Closed

Keywords for string, deprecate bin, oct, dec, hex, base #25717

wants to merge 11 commits into from

Conversation

bramtayl
Copy link
Contributor

No description provided.

@bramtayl bramtayl changed the title Base keywords Pad keyword for base, deprecate bin, oct, dec, hex Jan 24, 2018
@bramtayl
Copy link
Contributor Author

Based on the advice here: #25188 (comment)

HISTORY.md Outdated
@@ -417,7 +417,7 @@ Library improvements
respectively perform predicate function negation and function composition. For example,
`map(!iszero, (0, 1))` is now equivalent to `map(x -> !iszero(x), (0, 1))` and
`map(uppercase ∘ hex, 250:255)` is now equivalent to
`map(x -> uppercase(hex(x)), 250:255)` ([#17155]).
`map(x -> uppercase(base(16, x)), 250:255)` ([#17155]).
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this file should be left alone --- no need to try to rewrite history :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't tell Howard Zinn

assert_sixteen(b)
join([base(b,i,2) for i in id.val])
end
function Base.base(b, id::GitShortHash)
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure these methods should exist (same applies to the hex methods, which I was unaware of). Converting a GitHash to a string does the same thing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, let's just deprecate hex in favour of string.

Copy link
Sponsor Member

@JeffBezanson JeffBezanson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should also be removed from exports.jl

base/intfuncs.jl Outdated
```
"""
dec
base(b::Integer, n::Char; pad::Integer = 1) = base(b, UInt32(n); pad = pad)
Copy link
Sponsor Member

@JeffBezanson JeffBezanson Jan 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be removed (i.e. deprecated). Might as well require explicitly calling UInt32(c).

"""
raw(id::GitHash) -> Vector{UInt8}

Obtain the raw bytes of the [`GitHash`](@ref) as a vector of length $OID_RAWSZ.
"""
raw(id::GitHash) = collect(id.val)

Base.string(id::AbstractGitHash) = hex(id)
Base.string(id::AbstractGitHash) = base(16, id)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a base(::Int, ::AbstractGitHash) method defined somewhere?

What I meant in my previous comment was to define:

Base.string(id::GitHash) = join([base(i,2,pad=2) for i in id.val])
Base.string(id::GitShortHash) = string(id.hash)[1:id.len]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh got it I thought you meant these were already defined

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should actually be a method of print, then you also get string for free.

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We usually define string when it's actually easier to compute the String representation than to print it. This is quite rare, but if it can be implemented by calling base, this would be one of those cases.

@simonbyrne
Copy link
Contributor

Would it make sense to also provide some option to pad to the full width of the type (say pad=true), so that base(16,::UInt8,pad=true) is always 2 characters?

@JeffBezanson
Copy link
Sponsor Member

Let's avoid adding new features for now.

@StefanKarpinski
Copy link
Sponsor Member

Deprecating bin, oct, dec and hex is good – those were way too short and specific to be exported from Base. Perhaps base(b, n, pad=p) should be spelled string(n, base=b, pad=p)?

@bramtayl
Copy link
Contributor Author

That sounds better to me, especially because

string(x::Union{Int8,Int16,Int32,Int64,Int128}) = base(10, x)

@simonbyrne
Copy link
Contributor

I like that, especially since base isn't a verb, and it doesn't return the "base" of a number.

@StefanKarpinski
Copy link
Sponsor Member

Ok, let's go with that then! Delete all the verbs!!

@bramtayl
Copy link
Contributor Author

bramtayl commented Jan 25, 2018

Do we want to keep special hex printing for unsigned ints? In that case, the string method would have to be limited to Integers.

@StefanKarpinski
Copy link
Sponsor Member

Not a problem:

julia> string(0xff)
"255"

@rfourquet
Copy link
Member

julia> string(0xff)
"255"

OT, but I still don't understand why it works this way, intead of of returning "0xff"...

@JeffBezanson
Copy link
Sponsor Member

Because we don't consider 0x to be a canonical number format. print and string of integers just gives digit sequences. show and repr of unsigned numbers includes the 0x.

@StefanKarpinski
Copy link
Sponsor Member

As it turns out, this was a good choice as it allows us to fold base into string now :)

@StefanKarpinski StefanKarpinski added triage This should be discussed on a triage call and removed triage This should be discussed on a triage call labels Jan 25, 2018
@StefanKarpinski StefanKarpinski added this to the 1.0 milestone Jan 25, 2018
@StefanKarpinski StefanKarpinski self-assigned this Jan 25, 2018
"""
raw(id::GitHash) -> Vector{UInt8}

Obtain the raw bytes of the [`GitHash`](@ref) as a vector of length $OID_RAWSZ.
"""
raw(id::GitHash) = collect(id.val)

Base.string(id::AbstractGitHash) = hex(id)
Base.string(id::GitHash) = join([string(i, base = 16, pad = 2) for i in id.val])
Base.string(id::GitShortHash) = string(id.hash)[1:id.len]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the last discussion was to define Base.print(io::IO, id::GitHash) (which implicitly defines Base.string).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh no was it I'm so confused

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.g.

function Base.print(io::IO, id::GitHash)
    for i in id.val
        print(io, string(i, base = 16, pad = 2))
    end
end

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though this makes me think that we should also have the same args to print as well...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps best to leave that for another PR though

@bramtayl bramtayl changed the title Pad keyword for base, deprecate bin, oct, dec, hex Keywords for string, deprecate bin, oct, dec, hex, base Jan 26, 2018
@bramtayl
Copy link
Contributor Author

slayage

@StefanKarpinski
Copy link
Sponsor Member

StefanKarpinski commented Jan 26, 2018

It would probably be good to get this passing tests before making any further changes. Otherwise you're just making it harder to figure out which of the changes is causing the build failure.

@bramtayl
Copy link
Contributor Author

I just got through trying to build julia to help out with the debugging. These LoadError("sysimg.jl", are syntax errors?

@rfourquet
Copy link
Member

Because we don't consider 0x to be a canonical number format.

Thanks for taking the time to answer! But this moves my question to why 0x is not considered a canonical number format... when I work with unsigned numbers, it's usually more meaningful for me to read them as 0x... (but I may lack imagination to see why it's more generally useful to print them as decimal numbers). Also, print(true) prints true, but it's hard to argue that true would be a canonical number format.

@JeffBezanson
Copy link
Sponsor Member

People want some way to convert a number to just a digit string, with no decoration. It's less clear what to do with booleans.

base/intfuncs.jl Outdated
@@ -549,16 +549,14 @@ julia> ndigits(12345)
julia> ndigits(1022, 16)
3

julia> base(16, 1022)
julia> string(1022, bass = 16)
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

base

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rfourquet
Copy link
Member

People want some way to convert a number to just a digit string, with no decoration

dec ?

@JeffBezanson
Copy link
Sponsor Member

I don't understand the problem. If you want 0xff, use repr instead.

@JeffBezanson
Copy link
Sponsor Member

I'll try to rebase and fix this.

@JeffBezanson
Copy link
Sponsor Member

Rebased: #25804

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants