1
0
forked from aniani/vim

patch 8.2.1795: Vim9: operators && and || have a confusing result

Problem:    Vim9: operators && and || have a confusing result.
Solution:   Make the result a boolean.
This commit is contained in:
Bram Moolenaar
2020-10-03 22:52:39 +02:00
parent 92f26c256e
commit 2bb2658bef
12 changed files with 254 additions and 216 deletions

View File

@@ -154,25 +154,25 @@ Functions and variables are script-local by default ~
*vim9-scopes*
When using `:function` or `:def` to specify a new function at the script level
in a Vim9 script, the function is local to the script, as if "s:" was
prefixed. Using the "s:" prefix is optional. To define or use a global
function or variable the "g:" prefix should be used. For functions in an
autoload script the "name#" prefix is sufficient. >
prefixed. Using the "s:" prefix is optional. To define a global function or
variable the "g:" prefix must be used. For functions in an autoload script
the "name#" prefix is sufficient. >
def ThisFunction() # script-local
def s:ThisFunction() # script-local
def g:ThatFunction() # global
def ThatFunction() # global if no local ThatFunction()
def scriptname#function() # autoload
When using `:function` or `:def` to specify a new function inside a function,
the function is local to the function. It is not possible to define a
script-local function inside a function. It is possible to define a global
function, using the "g:" prefix.
When using `:function` or `:def` to specify a nested function inside a `:def`
function, this nested function is local to the code block it is defined in.
In a `:def` function IT is not possible to define a script-local function. it
is possible to define a global function by using the "g:" prefix.
When referring to a function and no "s:" or "g:" prefix is used, Vim will
prefer using a local function (in the function scope, script scope or
imported) before looking for a global function.
In all cases the function must be defined before used. That is when it is
first called or when `:defcompile` causes the call to be compiled.
imported) before looking for a global function. However, it is recommended to
always use "g:" to refer to a local function for clarity. In all cases the
function must be defined before used. That is when it is first called or when
`:defcompile` causes the call to be compiled.
The result is that functions and variables without a namespace can usually be
found in the script, either defined there or imported. Global functions and
@@ -184,7 +184,7 @@ and cannot be deleted or replaced.
Variable declarations with :var, :final and :const ~
*vim9-declaration*
*vim9-declaration* *:var*
Local variables need to be declared with `:var`. Local constants need to be
declared with `:final` or `:const`. We refer to both as "variables" in this
section.
@@ -261,7 +261,7 @@ Example: >
myList = [3, 4] # Error!
myList[0] = 9 # Error!
muList->add(3) # Error!
< *:final*
`:final` is used for making only the variable a constant, the value can be
changed. This is well known from Java. Example: >
final myList = [1, 2]
@@ -471,10 +471,6 @@ Conditions and expressions are mostly working like they do in JavaScript. A
difference is made where JavaScript does not work like most people expect.
Specifically, an empty list is falsy.
Any type of variable can be used as a condition, there is no error, not even
for using a list or job. This is very much like JavaScript, but there are a
few exceptions.
type TRUE when ~
bool v:true or 1
number non-zero
@@ -490,17 +486,25 @@ few exceptions.
class when not NULL
object when not NULL (TODO: when isTrue() returns v:true)
The boolean operators "||" and "&&" do not change the value: >
8 || 2 == 8
0 || 2 == 2
0 || '' == ''
8 && 2 == 2
0 && 2 == 0
2 && 0 == 0
[] && 2 == []
The boolean operators "||" and "&&" expect the values to be boolean, zero or
one: >
1 || false == true
0 || 1 == true
0 || false == false
1 && true == true
0 && 1 == false
8 || 0 Error!
'yes' && 0 Error!
[] || 99 Error!
When using `..` for string concatenation arguments of simple types are always
converted to string. >
When using "!" for inverting, there is no error for using any type and the
result is a boolean: >
!'yes' == false
var myList = [1, 2, 3]
!!myList == true
When using "`.."` for string concatenation arguments of simple types are
always converted to string. >
'hello ' .. 123 == 'hello 123'
'hello ' .. v:true == 'hello v:true'