What is the reason for this change? Will this not break many scripts that rely upon the traditional C approach of treating any nonzero value as true in a boolean context?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.![]()
Allowing for other numbers leads to mistakes. E.g. when using "-1", is that false or true? And using 9999 for true doesn't make sense, it is probably a mistake.
I guess because I've mostly programmed in C/C++, I think of any nonzero value as truthy. That just feels idiomatic and natural to me. For instance, if I had a variable intended to hold a line number (or zero to signify undefined), I would think it natural to treat the line number variable as a boolean to test for definedness. But I realize this sort of thing is often frowned upon in modern, strongly typed languages...
I didn't realize things would still work as before when the variable is used in a conditional. (A plugin user reported that the change was breaking something in a plugin I've contributed to, and I haven't actually tested with the latest version of Vim.)
Would the "want_bool" flag be true if unary '!' is used to force interpretation of a variable as a boolean? E.g., will something like this continue to work as before?
let valid_line_nr = !!line_nr
if valid_line_nr
...
endif
—
—
You are receiving this because you commented.
—
Bram Moolenaar wrote:
I just realized that what I mention actually would mean that the Vim9
behavior is wrong:let list = []
if !list
echo 'list is empty'
endifIn legacy Vim script this gives an error, in Vim9 script it doesn't...
In my own defence, the legacy Vim script error is weird:
E745: Using a List as a NumberOpinions?
Personally I prefer when a language steers clear of arbitrary,
unpredictable rules.
If it were a valid construct I could only know what if !list does
after consulting the language's documentation.
So, I think it would be better to return a type error (hopefully saying
"Using a List as a Boolean", though) and require using something like
if ! exists(list) and if ! empty(list).
I agree that this should be an error. In javascript an empty array is truthy, which is a constant source of errors and confusion.
var arr = []; console.log(!!arr); // true arr = null; console.log(!!arr); // false
I think a type error here would lead to better and more readable code.
I think what's confusing about the Javascript behavior is not the use of a collection in a boolean context per se, but that the "principle of least surprise" dictates that an empty collection of any kind should be treated as falsy, not truthy. Code like the following is actually quite intuitive:
if collection
" Do something with collection...
endif
I suspect that most programmers - even those unfamiliar with Vim script - would have a pretty good idea of what that construct does. For a syntactic construct with multiple, equally obvious interpretations, generating an error might be appropriate, but I can think of only one obvious interpretation of a collection in boolean context...
Brett S.
First of all, legacy Vim script and functions don't change. This only applies to Vim9 script and :def functions.
guns/vim-sexp has been broken since updating Vim past this patch:
Error detected while processing function sexp#leaf_flow:
line 34:
E1023: Using a Number as a Bool: 2
While I haven't bisected it to this patch, this patch does introduce E1023, so I presumed this patch is impacting legacy vim scripts.
ref: guns/vim-sexp#27
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or unsubscribe.![]()