Elvisfeddit.itimage Sleepzy ( @Sleepzy@feddit.it ) Programmer Humor@lemmy.ml • 1 month ago message-square16fedilinkarrow-up189
arrow-up189imageElvisfeddit.it Sleepzy ( @Sleepzy@feddit.it ) Programmer Humor@lemmy.ml • 1 month ago message-square16fedilink
minus-square QuazarOmega ( @QuazarOmega@lemy.lol ) linkfedilink12•1 month agoOn the left you have Elvis Presley, while on the right there’s the so-called Elvis operator
minus-square The Cuuuuube ( @Cube6392@beehaw.org ) linkfedilinkEnglish16•1 month agobeen programming since 2008. the fuck is an elvis operator?
minus-square Jerkface (any/all) ( @jerkface@lemmy.ca ) linkfedilinkEnglish8•1 month agoBeen programming since the 80s, ditto.
minus-square luciferofastora ( @luciferofastora@lemmy.zip ) linkfedilink4•1 month agoTernary if?then:else
minus-square The Cuuuuube ( @Cube6392@beehaw.org ) linkfedilinkEnglish4•1 month agogotacha. i’ve only ever heard them called ternaries. maybe i’m old. maybe i’m too young. definitely one of the two
minus-square QuazarOmega ( @QuazarOmega@lemy.lol ) linkfedilink8•1 month agoIt 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
minus-square dev_null ( @dev_null@lemmy.ml ) linkfedilink3•edit-21 month agoIt’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.
minus-square AVincentInSpace ( @AVincentInSpace@pawb.social ) linkfedilinkEnglish3•1 month agowhy would you call it anything other than the ternary operator
minus-square dev_null ( @dev_null@lemmy.ml ) linkfedilink3•edit-21 month agoBecause 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.
minus-square AVincentInSpace ( @AVincentInSpace@pawb.social ) linkfedilinkEnglish2•1 month agoThe second one isn’t valid syntax in any programming language I’m familiar with. What does it do?
minus-square dev_null ( @dev_null@lemmy.ml ) linkfedilink3•1 month agoIt’s a shorthand for writing this: variable = if (input != null) input else default This is equivalent: variable = input ?: default
minus-square AVincentInSpace ( @AVincentInSpace@pawb.social ) linkfedilinkEnglish2•edit-21 month agoHuh. Neat feature. That’s in C# I assume?
minus-square dev_null ( @dev_null@lemmy.ml ) linkfedilink2•edit-21 month agoIt’s in Kotlin and some other languages. C# has it but there it’s actually A ?? B.
minus-square QuazarOmega ( @QuazarOmega@lemy.lol ) linkfedilink2•1 month agoRead further down on my other comment to understand, it’s just how the operator looks
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?
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
It’s a shorthand for writing this:
This is equivalent:
The answers confusing it with the ternary operator are wrong.
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