Dominique wrote:
> Bram Moolenaar <[email protected]> wrote: > > > For the first example, we can use a new operator which is specifically > > for testing an expression to be falsy and using a replacement: > > > > var name = Getname() ?? 'unknown' > > > > Let me know if you have comments. > > This is known as the 'elvis operator', which exists > in several languages. See: > > https://en.wikipedia.org/wiki/Elvis_operator > > Generally, it is ?: as in: > > var name = Getname() ?: 'unknown'; > > It may be worth saying that it is almost equivalent to > the longer: > > var name = Getname() ? Getname() : 'unknown'; > > ... except that in the case of the elvis operator, > Gename() is guaranteed to be invoked only once, > which may not only be more efficient, but can also > be different if Getname() had side effects. > > This image explains why it's called the 'elvis operator': > > https://i.stack.imgur.com/bVG64.png What I intend to do here makes a difference between the two operators, which is also why I prefer using "??" instead of "?:". The latter suggest the equivalence with "cond ? expr : expr", which is not quite right. The condition in "cond ? expr : expr" is just like a condition used for "if": it should evaluate to a boolean. The ternary expression is a short way of doing if-then-else, thus using the same semantics for the condition makes sense. For the "??" operator, which I prefer calling the falsy-operator, the expression is not required to evaluate to a boolean. In fact, it's most useful if it doesn't. It can be a string, list, dict, etc. And evaluating it will never result in an error, every value is either truthy or falsy. A name sometimes used is "nullish coalescing operator", which is not only hard to type but also not quite the same. "nullish" and "falsy" have different semantics. We could call it the "falsy coalescing operator", but "falsy operator" works just as well and is a lot easier to type. Still, "nullish coalescing operator" comes closest, thus using "??" as the operator will be recognized by most users. I hope the different semantics won't cause too much confusion. -- hundred-and-one symptoms of being an internet addict: 34. You laugh at people with a 10 Mbit connection. /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net \\\ /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\ \\\ an exciting new programming language -- http://www.Zimbu.org /// \\\ help me help AIDS victims -- http://ICCF-Holland.org /// -- -- You received this message from the "vim_dev" maillist. Do not top-post! Type your reply below the text you are replying to. For more information, visit http://www.vim.org/maillist.php --- You received this message because you are subscribed to the Google Groups "vim_dev" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/vim_dev/202010051320.095DKWlx2406071%40masaka.moolenaar.net.
