github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/gen/expr/null_coalescing_op_doc.yaml (about) 1 - DocumentID: null-coalescing 2 Title: >- 3 `??` Null Coalescing Operator 4 CategoryID: parser 5 Summary: >- 6 Returns the right operand if the left operand is empty / undefined (expression) 7 Description: |- 8 The Null Coalescing operator is a little like a conditional where the result of the 9 operation is the first non-empty value from left to right. 10 11 An empty value is any of the following: 12 13 * an unset / undefined variable 14 * any value with a `null` data type 15 16 Other "falsy" values such as numerical values of `0`, boolean `false`, zero 17 length strings and strings containing `"null"` are not considered empty by the 18 null coalescing operator. 19 Examples: |- 20 **Assign a variable with a default value:** 21 22 ``` 23 » $foo = $bar ?? "baz" 24 ``` 25 26 If `$bar` is unset then the value of `$foo` will be **"baz"**. 27 28 **Multiple operators:** 29 30 ``` 31 » $unset_variable ?? null ?? "foobar" 32 foobar 33 ``` 34 Detail: |- 35 The following extract was taken from [Wikipedia](https://en.wikipedia.org/wiki/Null_coalescing_operator): 36 37 > The null coalescing operator (called the Logical Defined-Or operator in Perl) 38 > is a binary operator that is part of the syntax for a basic conditional 39 > expression in several programming languages. While its behavior differs 40 > between implementations, the null coalescing operator generally returns the 41 > result of its left-most operand if it exists and is not null, and otherwise 42 > returns the right-most operand. This behavior allows a default value to be 43 > defined for cases where a more specific value is not available. 44 > 45 > In contrast to the ternary conditional if operator used as `x ? x : y`, but 46 > like the binary Elvis operator used as `x ?: y`, the null coalescing operator 47 > is a binary operator and thus evaluates its operands at most once, which is 48 > significant if the evaluation of `x` has side-effects. 49 Related: 50 - expr 51 - is-null 52 - elvis 53 - pipe-err 54 - pipeline 55 - schedulers 56 - out 57 - err 58 - try 59 - trypipe 60 - logical-and 61 - logical-or 62 - "null"