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

Warn when comparing Char and String #47619

Closed
Timeroot opened this issue Nov 17, 2022 · 1 comment
Closed

Warn when comparing Char and String #47619

Timeroot opened this issue Nov 17, 2022 · 1 comment
Labels
duplicate Indicates similar issues or pull requests equality Issues relating to equality relations: ==, ===, isequal

Comments

@Timeroot
Copy link

More than once I've made a mistake like,

for line = lines
 headchar = line[1]
 if headchar == "X"
   break
 end
 ...
end

... and then it never stops because it starts with 'X', not "X". It usually takes me a minute to realize why it's not working.

How feasible would it be to special-case a warning when checking a character and string for equality? Sort of similar to how

5 in Dict(5 => "yes", 3 => "no")

produces a special warning,

AbstractDict collections only contain Pairs;
Either look for e.g. A=>B instead, or use the `keys` or `values`
function if you are looking for a key or value respectively.

Stacktrace:
 [1] error(s::String)
   @ Base .\error.jl:33
 [2] in(p::Int64, a::Dict{Int64, String})
   @ Base .\abstractdict.jl:28

I'm not familiar enough with the dispatch performance, but I could imagine that adding this check in the wrong place could impact performance heavily (by incurring a check+dispatch on every single instance of == anywhere). If the performance impact is minimal though, it might be handy.

@Seelengrab
Copy link
Contributor

Sadly a duplicate of #40717

@LilithHafner LilithHafner added duplicate Indicates similar issues or pull requests equality Issues relating to equality relations: ==, ===, isequal labels Nov 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate Indicates similar issues or pull requests equality Issues relating to equality relations: ==, ===, isequal
Projects
None yet
Development

No branches or pull requests

3 participants