3 ms·
OT but this is the first time I've seen the ternary operator used with only one operand. Apparently, it's only valid in GNU C though. https://en.wikipedia.org/
by adamnemecek 13y ago
OT but this is the first time I've seen the ternary operator used with only one operand. Apparently, it's only valid in GNU C though.
https://en.wikipedia.org/wiki/%3F:#C https://en.wikipedia.org/wiki/%3F:#C
- conradev 13y agoI've seen it used a lot in Objective-C for default values: NSNumber *value = dict[@"key"] ?: @1;
- cpeterso 13y ago?: is the "Elvis" operator.
- adamnemecek 13y agoThat's pretty a hilarious name for it. Is this from the Groovy community?
- vorg 13y agoThe standard name is the "Null-coalescing" operator, see http://en.wikipedia.org/wiki/Null_coalescing_operator http://en.wikipedia.org/wiki/Null_coalescing_operator . It's only called "Elvis" in Groovy. The name was announced one day by its Project Manager as part of a marketing campaign to make the name "G-String" mean a string on Elvis's guitar instead of a revealing item of clothing.
- erikig 13y agoYeah, it is a pretty nice shortcut. They also added it to PHP in 5.3 so you can do something like this now: $var = trim($_POST['var']) ?: 0;