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

in uses isequal (#9381) #24563

Closed
wants to merge 3 commits into from
Closed

in uses isequal (#9381) #24563

wants to merge 3 commits into from

Conversation

JackDevine
Copy link
Contributor

@JackDevine JackDevine commented Nov 10, 2017

I changed the docstring quite a bit, I think that it is now a good description of the current behavior. I also added the test in the first comment of #9381.

Edit: fix #9381

I changed the docstring quite a bit, I think that it is now a good description of the current behavior. I also added the test in the first comment of #9381.
@StefanKarpinski
Copy link
Member

There are quite a few more methods of in – did you check all of them?

@JackDevine
Copy link
Contributor Author

Thanks for pointing that out, I was just about to ask about that. For example, in associative.jl

function in(p::Pair, a::Associative, valcmp=(==))
    v = get(a,p[1],secret_table_token)
    if v !== secret_table_token
        valcmp(v, p[2]) && return true
    end
    return false
end

Do you want valcmp to become isequal by default? My understanding was that we are happy with how in works for dictionaries and sets. Also, would

in(x::Char, y::Char) = x == y

goes to

in(x::Char, y::Char) = isequal(x, y)

change anything?

When I look into this more, there are definitely a few that I need to change e.g. in(x::Number, y::Number). Should I only make changes where they might make a difference or should I just be greedy and make lots of changes?

@StefanKarpinski
Copy link
Member

StefanKarpinski commented Nov 10, 2017

Yes, I would change all of these. In some cases == behaves the same as isequal but if someone were to override or change the behavior of isequal but not == then we want this to be consistent everywhere. Thanks for tackling this!

@fredrikekre fredrikekre added breaking This change will break code collections Data structures holding multiple items, e.g. sets labels Nov 10, 2017
NEWS.md Outdated
@@ -272,6 +272,8 @@ This section lists changes that do not have deprecation warnings.
Library improvements
--------------------

* `in` now uses `isequal` to test for containment ([#9381]).
Copy link
Member

Choose a reason for hiding this comment

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

This should probably be listed under breaking changes instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, fixed.

@andyferris
Copy link
Member

I also wonder if == for a container should be isequal for all the elements. For example, [NaN] == [NaN] - well, they are the same container, no?. Like NaN, this is also important for null/missing.

@JackDevine
Copy link
Contributor Author

Is that idea relevant to this PR? I don't think that it is inconsistent to have in use isequal while still having [NaN] !== [NaN].

@andyferris
Copy link
Member

Is that idea relevant to this PR?

Not in the sense that this PR needs modifying. :)

However, I think there is a fair amount of relevance to #9381 (actually I should have probably put my comment there instead, sorry!) in that I would say that containers would be equal when they contain (i.e. element in container) the same elements. Obviously NaN in [NaN] is relevant to this PR, but we also have !([NaN] == [NaN]) but Set(NaN) == Set(NaN) which is exactly the kind of silliness that is being addressed here.

@@ -67,7 +67,7 @@ function next(v::ValueIterator, state)
n[1][2], n[2]
end

in(k, v::KeyIterator) = get(v.dict, k, secret_table_token) !== secret_table_token
in(k, v::KeyIterator) = !isequal(get(v.dict, k, secret_table_token), secret_table_token)
Copy link
Member

Choose a reason for hiding this comment

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

This is not quite right. This is using !== (which is the opposite of ===, not ==) to see if exactly the secret_table_token object was returned.

base/env.jl Outdated
@@ -4,7 +4,7 @@ if Sys.iswindows()
const ERROR_ENVVAR_NOT_FOUND = UInt32(203)

_getenvlen(var::Vector{UInt16}) = ccall(:GetEnvironmentVariableW,stdcall,UInt32,(Ptr{UInt16},Ptr{UInt16},UInt32),var,C_NULL,0)
_hasenv(s::Vector{UInt16}) = _getenvlen(s) != 0 || Libc.GetLastError() != ERROR_ENVVAR_NOT_FOUND
_hasenv(s::Vector{UInt16}) = !isequal(_getenvlen(s), 0) || !isequal(Libc.GetLastError(), ERROR_ENVVAR_NOT_FOUND)
Copy link
Member

Choose a reason for hiding this comment

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

These don't need to change.

base/env.jl Outdated
@@ -38,7 +38,7 @@ if Sys.iswindows()
end
else # !windows
_getenv(var::AbstractString) = ccall(:getenv, Cstring, (Cstring,), var)
_hasenv(s::AbstractString) = _getenv(s) != C_NULL
_hasenv(s::AbstractString) = !isequal(_getenv(s), C_NULL)
Copy link
Member

Choose a reason for hiding this comment

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

Also this.

base/show.jl Outdated
@@ -87,7 +87,7 @@ pipe_writer(io::IOContext) = io.io
lock(io::IOContext) = lock(io.io)
unlock(io::IOContext) = unlock(io.io)

in(key_value::Pair, io::IOContext) = in(key_value, io.dict, ===)
in(key_value::Pair, io::IOContext) = in(key_value, io.dict, isequal)
Copy link
Member

Choose a reason for hiding this comment

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

This should not change since the caller was specifically requesting ===.

@@ -44,7 +44,7 @@ function search(s::AbstractString, c::Chars, i::Integer)
end
search(s::AbstractString, c::Chars) = search(s,c,start(s))

in(c::Char, s::AbstractString) = (search(s,c)!=0)
in(c::Char, s::AbstractString) = !isequal(search(s,c), 0)
Copy link
Member

Choose a reason for hiding this comment

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

This should also not change, since it isn't comparing c; it's testing the result of a different function.

@JackDevine
Copy link
Contributor Author

Thanks @JeffBezanson , I may have gone a bit overboard with some of those changes. I have changed back to the old style in all of the places that you asked.

@JackDevine
Copy link
Contributor Author

Closing due to comment in #9381

@JackDevine JackDevine closed this Jan 4, 2018
@ararslan
Copy link
Member

ararslan commented Jan 4, 2018

Thank you regardless for your efforts here, @JackDevine!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking This change will break code collections Data structures holding multiple items, e.g. sets
Projects
None yet
Development

Successfully merging this pull request may close these issues.

should in use isequal to test for containment?
6 participants