空值结合运算符

空值结合运算符(在Perl称逻辑定义或运算符)是一种二元运算符,是多种编程语言的基本条件表达式语法的一部分,包括C#[1]、PowerShell 7.0.0、Perl 5.10,Swift,PHP 7.0.0。虽然其行为因实现而异,但空值合并运算符首先考虑返回其左操作数的值(如果左操作数存在且不为空) ,其次考虑返回右操作数的值。此行为可为特定值不可用的情况定义缺省值。

与三元条件表达式 x?x:y中表达式x可能求值2次相比,空值结合运算符更类似二元埃尔维斯运算符x?:y,操作数求值至多一次,这在x的求值有副作用情况下特别有意义。

编程语言用法 编辑

Bash 编辑

Bash中,“如果参数未设置或为空,则用缺省值替代”:[2]

#supplied_title='supplied title' # Uncomment this line to use the supplied title
title=${supplied_title:-'Default title'}
echo "$title" # prints: Default title

C# 编辑

C#中,空值结合运算符是??.常用于简化表达式:

possiblyNullValue ?? valueIfNull

例如,如果希望C#源码给一个页缺省页标题:

string pageTitle = suppliedTitle ?? "Default Title";

以代替更啰嗦的实现:

string pageTitle = (suppliedTitle != null) ? suppliedTitle : "Default Title";

或者

string pageTitle;

if (suppliedTitle != null)
{
    pageTitle = suppliedTitle;
}
else
{
    pageTitle = "Default Title";
}

上述3种实现给pageTitle相同的结果。

注意表达式suppliedTitle??运算符下求值1次,而在另外2种代码例子种可能求值2次。

该运算符在同一个表达式可以使用多次:

return some_Value ?? some_Value2 ?? some_Value3;

从C# 8.0起,支持??=空值结合赋值运算符:

some_Value ??= some_Value2;

以代替啰嗦的写法:

some_Value = some_Value ?? some_Value2;

结合使用空值条件运算符英语null-conditional operator?.或空值条件成员访问运算符?[],空值结合运算符可在对象为空或对象的成员为空时提供缺省值。例如,下述例子中page对象为空或者page不为空但其属性Title为空,则提供缺省值:

string pageTitle = page?.Title ?? "Default Title";

JavaScript 编辑

JavaScript最接近的运算符是??, 即"nullish coalescing operator",从ECMAScript第11版引入。[3]当左端操作数不为"nullish" (nullundefined),取其值作为结果,否则取右端操作数作为结果。 例如:

const a = b ?? 3;

逻辑或运算符(||)对任何布尔假值:null, undefined, "", 0, NaN, false,都会取右端操作数的值。

Python 编辑

Python没有空值结合运算符。可用条件表达式模拟其功能:

now() if time is None else time

Python的or运算符提供了类似但不同的行为。区别在于,如果左操作数的结果为Falseor也会返回右操作数的值:

42    or "something"  # returns 42
0     or "something"  # returns "something"
False or "something"  # returns "something"
""    or "something"  # returns "something"
None  or "something"  # returns "something"

而真正的空值结合运算符仅在最后一种情况下返回结果"something",而在左操作数为(0, False, "")时返回响应值。

PowerShell 编辑

PowerShell 7 提供了??空值结合运算符:[4]

$myVar = $null
$x = $myVar ?? "something" # assigns "something"

SQL 编辑

Oracle的PL/SQL, NVL()函数提供了这种输出:

NVL(possibly_null_value, 'value if null');

SQL Server/Transact-SQL有ISNULL函数,其原型为:

ISNULL(possibly_null_value, 'value if null');

注意ISNULLIS NULL不同,后者判断表达式是否为空。

ANSI SQL-92标准包括了一个COALESCE函数,在Oracle,[5] SQL Server,[6] PostgreSQL,[7] SQLite[8] and MySQL.[9]都被实现了。COALESCE函数返回第一个不为空的参数的结果,如果所有参数都为空,则返回空。

COALESCE(possibly_null_value[, possibly_null_value, ...]);

VB.NET 编辑

VB.NET中的If[10]运算符/关键字得到空值结合运算符的效果。

Dim pageTitle = If(suppliedTitle, "Default Title")

这比下述写法更精炼:

Dim pageTitle = If(suppliedTitle <> Nothing, suppliedTitle, "Default Title")

参考文献 编辑

  1. ^ BillWagner. ?? Operator (C# Reference). msdn.microsoft.com. [2021-08-23]. (原始内容存档于2017-03-17). 
  2. ^ Bash man page. [2021-08-30]. (原始内容存档于2019-12-27). 
  3. ^ ECMAScript 2020 Language Specification. Ecma International. June 2020 [2021-08-30]. (原始内容存档于2020-12-23). 
  4. ^ 引证错误:没有为名为:0的参考文献提供内容
  5. ^ Database SQL Language Reference. docs.oracle.com. [2021-08-30]. (原始内容存档于2021-08-30). 
  6. ^ COALESCE (SQL Server Compact). technet.microsoft.com. [2021-08-30]. (原始内容存档于2017-08-26). 
  7. ^ PostgreSQL: Documentation: 9.1: Conditional Expressions. www.postgresql.org. [2021-08-30]. (原始内容存档于2018-03-09). 
  8. ^ SQLite Query Language: Core Functions. www.sqlite.org. [2021-08-30]. (原始内容存档于2022-01-18). 
  9. ^ MySQL :: MySQL 5.5 Reference Manual :: 12.3.2 Comparison Functions and Operators. dev.mysql.com. [2021-08-30]. (原始内容存档于2019-12-26). 
  10. ^ dotnet-bot. If Operator (Visual Basic). docs.microsoft.com. [2021-08-30]. (原始内容存档于2022-01-21).