vint icon indicating copy to clipboard operation
vint copied to clipboard

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

Open w0rp opened this issue 8 years ago • 5 comments

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.

w0rp avatar Aug 08 '17 07:08 w0rp

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...

brammool avatar Jun 19 '20 19:06 brammool

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.

Houl avatar Jun 23 '20 17:06 Houl

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.

w0rp avatar Jun 25 '20 09:06 w0rp

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.

There are arguments both for ignoring types and for strict type checking. I don't plan to change this for legacy Vim script. In Vim9 the plan is to have reasonable strict type checking. Thus comparing a number with a string will give an error. In most cases that's the right thing to do. You can use "string(theNumber) == theString" if you want to. Or use string() on both sides if you don't know what the type will be.

-- What do you get when you cross a joke with a rehtorical question?

/// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net \
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \
\\ an exciting new programming language -- http://www.Zimbu.org /// \\ help me help AIDS victims -- http://ICCF-Holland.org ///

brammool avatar Jun 25 '20 20:06 brammool

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

w0rp avatar Jul 04 '20 11:07 w0rp