121
Elvis
(feddit.it)
Post funny things about programming here! (Or just rant about your favourite programming language.)
On the left you have Elvis Presley, while on the right there's the so-called Elvis operator
been programming since 2008. the fuck is an elvis operator?
It's a shorthand for writing this:
This is equivalent:
The answers confusing it with the ternary operator are wrong.
Been programming since the 80s, ditto.
Ternary if?then:else
gotacha. i've only ever heard them called ternaries. maybe i'm old. maybe i'm too young. definitely one of the two
It specifically refers to this shorthand
?:
that works like this:The condition is also the value if it is truthy
why would you call it anything other than the ternary operator
Because it's not one. Ternary operator is A ? B : C, Elvis operator is A ?: B. The same two characters are involved, but both the syntax and effect is different.
The second one isn't valid syntax in any programming language I'm familiar with. What does it do?
It's a shorthand for writing this:
This is equivalent:
Huh. Neat feature. That's in C# I assume?
It's in Kotlin and some other languages. C# has it but there it's actually
A ?? B
.Read further down on my other comment to understand, it's just how the operator looks