You're viewing a single thread.
I don't understand this. Small brained users rise up
15 0 ReplyOn the left you have Elvis Presley, while on the right there's the so-called Elvis operator
13 0 Replybeen programming since 2008. the fuck is an elvis operator?
16 0 ReplyBeen programming since the 80s, ditto.
8 0 ReplyIt's a shorthand for writing this:
variable = if (input != null) input else default
This is equivalent:
variable = input ?: default
The answers confusing it with the ternary operator are wrong.
3 0 ReplyTernary if?then:else
3 0 Replygotacha. i've only ever heard them called ternaries. maybe i'm old. maybe i'm too young. definitely one of the two
4 0 ReplyIt specifically refers to this shorthand
?:
that works like this:$value = $thing_that_could_be_truthy ?: 'fallback value'; # same as $value = $thing_that_could_be_truthy ? $thing_that_could_be_truthy : 'fallback value';
The condition is also the value if it is truthy
8 0 Reply
why would you call it anything other than the ternary operator
3 0 ReplyBecause 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.
3 0 ReplyThe second one isn't valid syntax in any programming language I'm familiar with. What does it do?
2 0 ReplyIt's a shorthand for writing this:
variable = if (input != null) input else default
This is equivalent:
variable = input ?: default
4 0 ReplyHuh. Neat feature. That's in C# I assume?
2 0 ReplyIt's in Kotlin and some other languages. C# has it but there it's actually
A ?? B
.2 0 Reply
Read further down on my other comment to understand, it's just how the operator looks
2 0 Reply