fbpx
维基百科

埃尔维斯运算符

编程语言中,埃尔维斯运算符是一种二元运算符,常写为?:or||,如果第一操作数求值为真则返回其值,否则返回第二操作数的值。类似于带有“最后值”语义的短路求值。埃尔维斯运算符是受三元条件运算符 ? :启发而被提出的。因为埃尔维斯运算符表达式A ?: B近似于三元条件运算符A ? A : B

埃尔维斯运算符得名于它的通常表示法?:,相似于埃尔维斯·普雷斯利(即“猫王”)的颜文字侧脸的额发英语quiff,或者其他角度看相遇于他的得意的笑脸。[1]

有一个相似的运算符是空值结合运算符,把布尔真值检查改为是否为空指针null。常写为??,如C#[2]

编程语言支持 编辑

  • GNU CC++的语言扩展,从2001年3月的GCC 2.95.3版本开始,三元运算符的第二操作数是可选的。[3]看起来这是最早的埃尔维斯运算符。[4]
  • Apache Groovy的埃尔维斯运算符?:是个独特的运算符。[5]起自2007年12月的Groovy 1.5[6]。不同于GNU C和PHP,Groovy不是简单地允许三元运算符?:第二操作数可忽略,而是?:作为单独的二元运算符其间不能有空格符。
  • PHP 5.3开始,允许三元运算符的中间部分可留空。[7] (June 2009).
  • Fantom语言英语Fantom (programming language)?:二元运算符,比较第一操作数是否为null
  • Kotlin语言的埃尔维斯运算符的左操作数的值不为null则返回其值, 否则返回右操作数的值。[8] 常用于return,如:val foo = bar() ?: return
  • Gosu语言英语Gosu (programming language)?:运算符
  • C#安全导航运算符?.是一种埃尔维斯运算符,[9]。但C#的空值结合运算符??才是相当于别的语言的?:的埃尔维斯运算符。
  • Adobe ColdFusionCFML英语CFML的埃尔维斯运算符是?:
  • Xtend英语Xtend有埃尔维斯运算符.[10]
  • GoogleClosure Templates英语Google Closure Tools#Closure Templates的埃尔维斯运算符是空值结合运算符,等效于isNonnull($a) ? $a : $b[11]
  • Swift的Nil结合运算符??[12],例如(a ?? b).
  • SQL的COALESCE函数,例如COALESCE(a, b)
  • Ballerina英语Ballerina (programming language)的埃尔维斯运算符L ?: R[13]
  • Clojure支持or[14]宏,如(or a b)。这是操作数个数可变的操作符而不是二元操作符,例如(or a b c d e)将返回第一个非假的操作数的值。
  • Dart语言提供了??运算符
  • JavaScript空值结合运算符??,例如(a ?? b)[15]
  • TypeScript从版本3.7开始[16],如同JavaScript开始支持埃尔维斯运算符。
  • Lua支持or[17]逻辑运算符,如(a or b).

类似的短路运算符OR 编辑

Common Lisp, Clojure, Lua, Object Pascal, Perl, Python, Ruby, JavaScript等编程语言中, OR运算符(典型为||or)都是短路行为:如果左操作数的值为真,则表达式结果为真,不再对右操作数求值。这被称作短路求值

C/C++语言标准强制||&&短路求值运算符。

参见 编辑

参考文献 编辑

  1. ^ Joyce Farrell. Java Programming. 7 February 2013: 276. ISBN 978-1285081953. The new operator is called Elvis operator because it uses a question mark and a colon together (?:); if you view it sideways, it reminds you of Elvis Presley. 
  2. ^ ?? Operator. C# Reference. Microsoft. [5 December 2018]. (原始内容于2022-02-07). 
  3. ^ Using the GNU Compiler Collection (GCC): Conditionals. gcc.gnu.org. [2022-03-05]. (原始内容于2022-01-12). 
  4. ^ Using and Porting the GNU Compiler Collection (GCC): C Extensions. gcc.gnu.org. [2022-03-05]. (原始内容于2021-07-14). 
  5. ^ Elvis Operator (?: ). [2022-03-05]. (原始内容于2021-08-16). 
  6. ^ The Apache Groovy programming language - Groovy 1.5 release notes. groovy-lang.org. [2022-03-05]. (原始内容于2022-02-11). 
  7. ^ PHP: Comparison Operators - Manual. PHP website. [2014-02-17]. (原始内容于2022-02-28). 
  8. ^ Null Safety - Kotlin Programming Language. Kotlin. [2022-03-05]. (原始内容于2022-02-13). 
  9. ^ Albahari, Joseph; Albahari, Ben. C# 6.0 in a Nutshell 6. O'Reilly Media. 2015: 59. ISBN 978-1491927069. 
  10. ^ Efftinge, Sven. Xtend - Expressions. eclipse.org. [2022-03-05]. (原始内容于2021-05-06). 
  11. ^ Closure Templates - Expressions. GitHub. 29 October 2021 [2022-03-05]. (原始内容于2022-03-05). 
  12. ^ The Swift Programming Language (Swift 4.1): Basic Operators. developer.apple.com. [2022-03-05]. (原始内容于2017-11-12). 
  13. ^ . Ballerina. [2018-12-19]. (原始内容存档于2018-12-20). 
  14. ^ clojure.core or macro API reference. [2022-03-05]. (原始内容于2022-02-25). 
  15. ^ Nullish coalescing operator (??). mozilla. [2021-01-28]. (原始内容于2022-02-28) (英语). 
  16. ^ nullish coalescing commit by Kingwl · Pull Request #32883 · microsoft/TypeScript. GitHub. [2019-10-08]. (原始内容于2020-11-12) (英语). 
  17. ^ Lua or operator Reference. [2022-03-05]. (原始内容于2022-01-30). 

埃尔维斯运算符, 编程语言中, 是一种二元运算符, 常写为, 如果第一操作数求值为真则返回其值, 否则返回第二操作数的值, 类似于带有, 最后值, 语义的短路求值, 是受三元条件运算符, 启发而被提出的, 因为表达式a, b近似于三元条件运算符a, 得名于它的通常表示法, 相似于埃尔维斯, 普雷斯利, 猫王, 的颜文字侧脸的额发, 英语, quiff, 或者其他角度看相遇于他的得意的笑脸, 有一个相似的运算符是空值结合运算符, 把布尔真值检查改为是否为空指针null, 常写为, 如c, 目录, 编程语言支持, 类似. 编程语言中 埃尔维斯运算符是一种二元运算符 常写为 or 如果第一操作数求值为真则返回其值 否则返回第二操作数的值 类似于带有 最后值 语义的短路求值 埃尔维斯运算符是受三元条件运算符 启发而被提出的 因为埃尔维斯运算符表达式A B近似于三元条件运算符A A B 埃尔维斯运算符得名于它的通常表示法 相似于埃尔维斯 普雷斯利 即 猫王 的颜文字侧脸的额发 英语 quiff 或者其他角度看相遇于他的得意的笑脸 1 有一个相似的运算符是空值结合运算符 把布尔真值检查改为是否为空指针null 常写为 如C 2 目录 1 编程语言支持 2 类似的短路运算符OR 3 参见 4 参考文献编程语言支持 编辑GNU C和C 的语言扩展 从2001年3月的GCC 2 95 3版本开始 三元运算符的第二操作数是可选的 3 看起来这是最早的埃尔维斯运算符 4 Apache Groovy的埃尔维斯运算符 是个独特的运算符 5 起自2007年12月的Groovy 1 5 6 不同于GNU C和PHP Groovy不是简单地允许三元运算符 第二操作数可忽略 而是 作为单独的二元运算符其间不能有空格符 从PHP 5 3开始 允许三元运算符的中间部分可留空 7 June 2009 Fantom语言 英语 Fantom programming language 的 二元运算符 比较第一操作数是否为null Kotlin语言的埃尔维斯运算符的左操作数的值不为null则返回其值 否则返回右操作数的值 8 常用于return 如 val foo bar return Gosu语言 英语 Gosu programming language 运算符 C 的安全导航运算符 是一种埃尔维斯运算符 9 但C 的空值结合运算符 才是相当于别的语言的 的埃尔维斯运算符 Adobe ColdFusion和CFML 英语 CFML 的埃尔维斯运算符是 Xtend 英语 Xtend 有埃尔维斯运算符 10 GoogleClosure Templates 英语 Google Closure Tools Closure Templates 的埃尔维斯运算符是空值结合运算符 等效于isNonnull a a b 11 Swift的Nil结合运算符 12 例如 a b SQL的COALESCE函数 例如COALESCE a b Ballerina 英语 Ballerina programming language 的埃尔维斯运算符L R 13 Clojure支持or 14 宏 如 or a b 这是操作数个数可变的操作符而不是二元操作符 例如 or a b c d e 将返回第一个非假的操作数的值 Dart语言提供了 运算符 JavaScript的空值结合运算符 例如 a b 15 TypeScript从版本3 7开始 16 如同JavaScript开始支持埃尔维斯运算符 Lua支持or 17 逻辑运算符 如 a or b 类似的短路运算符OR 编辑在Common Lisp Clojure Lua Object Pascal Perl Python Ruby JavaScript等编程语言中 OR运算符 典型为 或or 都是短路行为 如果左操作数的值为真 则表达式结果为真 不再对右操作数求值 这被称作短路求值 C C 语言标准强制 和 amp amp 是短路求值运算符 参见 编辑 或更广范围的条件运算符 用作三元运算符 安全导航运算符 常为 三路比较 lt gt 可选类型参考文献 编辑 Joyce Farrell Java Programming 7 February 2013 276 ISBN 978 1285081953 The new operator is called Elvis operator because it uses a question mark and a colon together if you view it sideways it reminds you of Elvis Presley Operator C Reference Microsoft 5 December 2018 原始内容存档于2022 02 07 Using the GNU Compiler Collection GCC Conditionals gcc gnu org 2022 03 05 原始内容存档于2022 01 12 Using and Porting the GNU Compiler Collection GCC C Extensions gcc gnu org 2022 03 05 原始内容存档于2021 07 14 Elvis Operator 2022 03 05 原始内容存档于2021 08 16 The Apache Groovy programming language Groovy 1 5 release notes groovy lang org 2022 03 05 原始内容存档于2022 02 11 PHP Comparison Operators Manual PHP website 2014 02 17 原始内容存档于2022 02 28 Null Safety Kotlin Programming Language Kotlin 2022 03 05 原始内容存档于2022 02 13 Albahari Joseph Albahari Ben C 6 0 in a Nutshell 6 O Reilly Media 2015 59 ISBN 978 1491927069 Efftinge Sven Xtend Expressions eclipse org 2022 03 05 原始内容存档于2021 05 06 Closure Templates Expressions GitHub 29 October 2021 2022 03 05 原始内容存档于2022 03 05 The Swift Programming Language Swift 4 1 Basic Operators developer apple com 2022 03 05 原始内容存档于2017 11 12 Elvis Operator Ballerina Programming Language Ballerina 2018 12 19 原始内容存档于2018 12 20 clojure core or macro API reference 2022 03 05 原始内容存档于2022 02 25 Nullish coalescing operator mozilla 2021 01 28 原始内容存档于2022 02 28 英语 nullish coalescing commit by Kingwl Pull Request 32883 microsoft TypeScript GitHub 2019 10 08 原始内容存档于2020 11 12 英语 Lua or operator Reference 2022 03 05 原始内容存档于2022 01 30 取自 https zh wikipedia org w index php title 埃尔维斯运算符 amp oldid 72574281, 维基百科,wiki,书籍,书籍,图书馆,

文章

,阅读,下载,免费,免费下载,mp3,视频,mp4,3gp, jpg,jpeg,gif,png,图片,音乐,歌曲,电影,书籍,游戏,游戏。