aboutsummaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
authorzeertzjq <zeertzjq@outlook.com>2023-04-14 11:00:17 +0800
committerzeertzjq <zeertzjq@outlook.com>2023-04-14 18:04:09 +0800
commitd6e2804ab4f8810293dbcd748bfb938d9e0c3d52 (patch)
treeee140b0eee513de0aad0cf6cbd55d56f4799dd7f /runtime
parent7caf0eafd83b5a92f2ff219b3a64ffae4174b9af (diff)
downloadrneovim-d6e2804ab4f8810293dbcd748bfb938d9e0c3d52.tar.gz
rneovim-d6e2804ab4f8810293dbcd748bfb938d9e0c3d52.tar.bz2
rneovim-d6e2804ab4f8810293dbcd748bfb938d9e0c3d52.zip
vim-patch:8.2.1794: no falsy Coalescing operator
Problem: No falsy Coalescing operator. Solution: Add the "??" operator. Fix mistake with function argument count. https://github.com/vim/vim/commit/92f26c256e06277ff2ec4ce7adea1eb58c85abe0 Cherry-pick tv2bool() into eval/typval.c. Cherry-pick *??* tag from Vim runtime. Co-authored-by: Bram Moolenaar <Bram@vim.org>
Diffstat (limited to 'runtime')
-rw-r--r--runtime/doc/eval.txt46
1 files changed, 43 insertions, 3 deletions
diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt
index fe15ba6115..9f0a6ceb25 100644
--- a/runtime/doc/eval.txt
+++ b/runtime/doc/eval.txt
@@ -93,7 +93,27 @@ non-zero number it means TRUE: >
:" executed
To test for a non-empty string, use empty(): >
:if !empty("foo")
-<
+
+< *falsy* *truthy*
+An expression can be used as a condition, ignoring the type and only using
+whether the value is "sort of true" or "sort of false". Falsy is:
+ the number zero
+ empty string, blob, list or dictionary
+Other values are truthy. Examples:
+ 0 falsy
+ 1 truthy
+ -1 truthy
+ 0.0 falsy
+ 0.1 truthy
+ '' falsy
+ 'x' truthy
+ [] falsy
+ [0] truthy
+ {} falsy
+ #{x: 1} truthy
+ 0z falsy
+ 0z00 truthy
+
*non-zero-arg*
Function arguments often behave slightly different from |TRUE|: If the
argument is present and it evaluates to a non-zero Number, |v:true| or a
@@ -841,9 +861,12 @@ All expressions within one level are parsed from left to right.
------------------------------------------------------------------------------
-expr1 *expr1* *ternary* *E109*
+expr1 *expr1* *ternary* *falsy-operator* *??* *E109*
+
+The ternary operator: expr2 ? expr1 : expr1
+The falsy operator: expr2 ?? expr1
-expr2 ? expr1 : expr1
+Ternary operator ~
The expression before the '?' is evaluated to a number. If it evaluates to
|TRUE|, the result is the value of the expression between the '?' and ':',
@@ -866,6 +889,23 @@ To keep this readable, using |line-continuation| is suggested: >
You should always put a space before the ':', otherwise it can be mistaken for
use in a variable such as "a:1".
+Falsy operator ~
+
+This is also known as the "null coalescing operator", but that's too
+complicated, thus we just call it the falsy operator.
+
+The expression before the '??' is evaluated. If it evaluates to
+|truthy|, this is used as the result. Otherwise the expression after the '??'
+is evaluated and used as the result. This is most useful to have a default
+value for an expression that may result in zero or empty: >
+ echo theList ?? 'list is empty'
+ echo GetName() ?? 'unknown'
+
+These are similar, but not equal: >
+ expr2 ?? expr1
+ expr2 ? expr2 : expr1
+In the second line "expr2" is evaluated twice.
+
------------------------------------------------------------------------------
expr2 and expr3 *expr2* *expr3*