0 points

I’m learning swift and I actually just discovered ?? today. Am I missing out in other languages?

permalink
report
reply
0 points

Yes, it’s very useful when applied correctly.

I’m always disappointed when I remember, that I can’t use such a feature, because I’m stuck using Java.

permalink
report
parent
reply
0 points

Maybe you’r using an older version of Java? Works fine for me.

permalink
report
parent
reply
0 points
*

New versions of java have a null coalescing operator?

I didn’t know that.

Edit: a short search didn’t return any answers, as far as I can see java doesn’t have this operator, the closest thing is the ternary if operator.

permalink
report
parent
reply
0 points

I enjoy this:

return a.or(b);

But yeah, that requires an Option type rather than null pointers…

permalink
report
reply
0 points

Is that Rust? Assuming a is an Option (which would be close approximation of OP’s nullable type) and assuming b is not null, then this would be closer to the original idea:

a.unwrap_or(b)

It returns value of a if it’s not None rather than Option.

permalink
report
parent
reply
0 points

Ah, true. Thanks.

Theoretically, it was supposed to be pseudo-code, secretly inspired by Rust, but I did get that one mixed up.

And I am actually even a fan of the word unwrap there, because it follows a schema and you can have your IDE auto-complete all the things you can do with an Option.
In many of these other languages, you just get weird collections of symbols which you basically have to memorize and which only cover rather specific uses.

permalink
report
parent
reply
0 points
*

return a and a or breturn a or b

correction from @murtaza64

permalink
report
reply
0 points

Lua. My beloved.

permalink
report
parent
reply
0 points
*

Ruby:

a || b

(no return as last line is returned implicitly, no semicolon)

EDIT: As pointed out in the comments, this is not strictly equivalent, as it will return b if a is false as well as if it’s nil (these are the only two falsy values in Ruby).

permalink
report
reply
0 points

Python:

return a or b

i like it because it reads like a sentence so it somewhat makes sense

and you can make it more comprehensive if you want to:

return a if a is not None else b

permalink
report
parent
reply
0 points
*

This diverges from the OP code snippets if a has the value False.

permalink
report
parent
reply
0 points

My coworker flips his shit every time I include a ternary operator in a PR. He also insists on refactoring any block of code longer than two lines into its own function, even when it’s only used once.

He is not well liked.

permalink
report
reply
0 points

He also insists on refactoring any block of code longer than two lines into its own function

Thanks, uncle Bob.

permalink
report
parent
reply

Programmer Humor

!programmerhumor@lemmy.ml

Create post

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

  • Posts must be relevant to programming, programmers, or computer science.
  • No NSFW content.
  • Jokes must be in good taste. No hate speech, bigotry, etc.

Community stats

  • 6.7K

    Monthly active users

  • 796

    Posts

  • 7.3K

    Comments

Community moderators