- bl4kers ( @bl4kers@lemmy.ml ) English12•1 month ago
I don’t understand this. Small brained users rise up
- QuazarOmega ( @QuazarOmega@lemy.lol ) 12•1 month ago
On the left you have Elvis Presley, while on the right there’s the so-called Elvis operator
- The Cuuuuube ( @Cube6392@beehaw.org ) English16•1 month ago
been programming since 2008. the fuck is an elvis operator?
- Jerkface (any/all) ( @jerkface@lemmy.ca ) English8•1 month ago
Been programming since the 80s, ditto.
- luciferofastora ( @luciferofastora@lemmy.zip ) 4•1 month ago
Ternary if?then:else
- The Cuuuuube ( @Cube6392@beehaw.org ) English4•1 month ago
gotacha. i’ve only ever heard them called ternaries. maybe i’m old. maybe i’m too young. definitely one of the two
- QuazarOmega ( @QuazarOmega@lemy.lol ) 8•1 month ago
It 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
- dev_null ( @dev_null@lemmy.ml ) 3•1 month ago
It’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.
- AVincentInSpace ( @AVincentInSpace@pawb.social ) English3•1 month ago
why would you call it anything other than the ternary operator
- dev_null ( @dev_null@lemmy.ml ) 3•1 month ago
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.
- AVincentInSpace ( @AVincentInSpace@pawb.social ) English2•1 month ago
The second one isn’t valid syntax in any programming language I’m familiar with. What does it do?
- dev_null ( @dev_null@lemmy.ml ) 3•1 month ago
It’s a shorthand for writing this:
variable = if (input != null) input else default
This is equivalent:
variable = input ?: default
- AVincentInSpace ( @AVincentInSpace@pawb.social ) English2•1 month ago
Huh. Neat feature. That’s in C# I assume?
- dev_null ( @dev_null@lemmy.ml ) 2•1 month ago
It’s in Kotlin and some other languages. C# has it but there it’s actually
A ?? B
.
- QuazarOmega ( @QuazarOmega@lemy.lol ) 2•1 month ago
Read further down on my other comment to understand, it’s just how the operator looks
- NicKoehler ( @NicKoehler@feddit.it ) 2•1 month ago
Same