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

Prefer is# and is? instead of ==# and ==? #233

Open
w0rp opened this issue Aug 8, 2017 · 5 comments
Open

Prefer is# and is? instead of ==# and ==? #233

w0rp opened this issue Aug 8, 2017 · 5 comments
Labels

Comments

@w0rp
Copy link

w0rp commented Aug 8, 2017

When you compare expressions with String literals, Vint will tell you to use ==# or ==? instead of ==, as == changes depending on user configuration, and Vint will do the same with is and strings, preferring is# and is?. I think Vint should go one step further and disallow ==# or ==? for String literal comparisons too, and prefer is# or is? instead.

If you compare 0 with any string which does not contain numbers using ==# or ==?, The comparison will return 1. This is because with something like 0 ==# 'foo', Vim converts the RHS to a Number, and that conversion yields 0, so the result is 1. This is almost certainly not what you want. This has been a source of bugs for my project.

I would argue that if you're making a comparison between any value and a String, what you want is for that comparison to be true only if both sides of a comparison are String values. I think there's pretty much no reason to ever use ==# or ==?, and you should use is# or is? instead.

@brammool
Copy link

We can't change this for legacy Vim script, it would break existing scripts.
It is changed for Vim9 script, which checks types more strict.
So perhaps suggest using Vim9 script? Too early for that...

@Houl
Copy link

Houl commented Jun 23, 2020

Problem with is#:

:echo "a" is# [1,2,3]
=> 0

happily compares string and list without error. I mostly want to get an error here. And :echo ["a"] ==# [[1,2,3]] isn't any better. So there is no clear winner.

@w0rp
Copy link
Author

w0rp commented Jun 25, 2020

I like being able to compare Strings and Lists without getting an error. What I did in the ALE codebase is add uses of grep to ask you to replace == and friends with is and friends, and there have been fewer bugs ever since.

@brammool
Copy link

brammool commented Jun 25, 2020 via email

@w0rp
Copy link
Author

w0rp commented Jul 4, 2020

That sounds horrible, and I hope it doesn't catch on.

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

No branches or pull requests

4 participants