Is your feature request about something that is currently impossible or hard to do? Please describe the problem.
The doc of vim9 says "Any type of variable can be used as a condition", but they cannot be assigned or returned as a boolean unless they are cast explicitly.
Moreover, every vim builtin functions returns 1 or 0 instead of a boolean, so you frequently need to cast them to boolean.
I wonder if this implicit casting will be implemented to vim9.
Following script works only when g:foo exists AND is v:true or v:false, otherwise E1029 is thrown.
def s:is_foo(): bool return exists('g:foo') && g:foo enddef echo s:is_foo()
Describe the solution you'd like
Cast any expressions to boolean implicitly.
Describe alternatives you've considered
!!(exists('g:foo') && g:foo) does work, but I feel it's redundant.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.![]()
Yes, there is a balance to be found here between type-safety and convenience.
We have already made it so that when a function argument is true/false one can also use zero and one.
It is also unlikely that this leads to mistakes, so long as we don't accept other numbers.
bool flag = 1 # looks like true
bool flag = 99 # must be a mistake, type should be number
bool flag = -1 # not sure what the intention was here
For conditions it's clear that we need a true/false outcome. Here we could also be strict, but following Typescript, like we do in several other places, loose type checking makes it easier to write. This uses the concept of Truthy and Falsy. Personally I have a slight preference to be more strict, but keeping in line with Typescript is more important.
We do fix one inconsistency of Typescript: an empty string is Falsy while an empty list/array is Truthy. This is a mistake, probably based on historical reasons.
I didn't address your specific return statement. I wonder if we can say that if && or || is used, then the user clearly intends to get a boolean result, thus we can allow for automatic conversion to a bool type.
Not sure how to implement that though. Could probably make it work in compiled code, but we also need to be able to do this in Vim9 script. Hmm, perhaps we can use a flag in the v_lock field. Then we can do this:
let flag: bool = 99 # error!
let flag: bool = 'yes' # error!
let flag: bool = 99 && 'yes' # OK
Or is that going in the direction of being weird again?
Thank you! Sounds good solution!