this post was submitted on 08 Dec 2023
623 points (96.4% liked)

Programmer Humor

32375 readers
1154 users here now

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

Rules:

founded 5 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 33 points 11 months ago (2 children)

I enjoy this:

return a.or(b);

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

[–] [email protected] 6 points 11 months ago (1 children)
[–] [email protected] 10 points 11 months ago
[–] [email protected] 6 points 11 months ago (1 children)

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.

[–] [email protected] 1 points 11 months ago

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.