0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

貓王運算子 - Elvis Operator

Last updated at Posted at 2017-07-02

?:

就是這個玩意兒

看 Kotline 的書看到這個運算子,
就決定查一下他叫什麼

似乎是從 Java 拿過來的一個運算子

作用

var a = left-expression() ?: right-expression()

他的用法就是,

  • ?: 的左側 不是 null 的時候會回傳左側的運算結果
  • ?: 的左側 null 的時候會回傳右側的運算結果

所以其實也可以理解成是 ?: 的右側為左側 nullable 的運算結果提供了一個「預設值」

Swift - Optional Chain

在 Swift 的話,就是這個符號

??

可以為 optional type 提供一個預設值

名字的由來 - 貓王?

頭稍往左旋轉 90 度,
就可以發現這個符號變成一個有著飛機頭的臉

似乎是這樣就被叫做 Elvis Operator 了 :laughing:

參考

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?