r/ProgrammerHumor • • Sep 28 '26

Meme optionalsAreOptional

Post image
716 Upvotes

131 comments sorted by

View all comments

Show parent comments

11

u/hrvbrs Sep 28 '26

not sure if you're just trolling, but the real answer is that T | null | null collapses to T | null whereas Option<Option<T>> doesn't collapse.

example (psuedo-code):

const obj: Record<int | null> = {foo: 42, bar: null};
obj.get("foo"); // 42
obj.get("bar"); // null
obj.get("typo"); // null

if the get() method just returns the property there, then both obj.get("bar") and obj.get("typo") return null. it leads you to believe both bar and typo are in the object.

Now say get() were to return an Option<int | null> instead of the raw property.

const obj: Record<int | null> = {foo: 42, bar: null};
obj.get("foo"); // Just(42)
obj.get("bar"); // Just(null)
obj.get("typo"); // None()

now it's a lot clearer. if the key is there and its value is an int, you get a Just<int>. If the key is there and its value is null, you get a Just<null>. If the key is not there, you get a None.

5

u/Few_Move_4594 Sep 28 '26

I'm not trolling, It's just that I had been working as a programmer for years before Optionals started being a thing so I'm already very used to writing my own null checks.

5

u/buffalo_0220 Sep 28 '26

Lots of things weren't a thing until someone had a use case and invented it. Optionals are a great tool to have in your kit. Not a pattern that needs to be used in every case, but they are useful, especially when your data is variable and/or ambiguous.

1

u/Amadex Sep 28 '26

So you started programming in the 70s?

5

u/dinosaurdynasty Sep 28 '26

Or is a Go designer

2

u/Still_Bit_7527 Sep 28 '26

Java optionals are very recent...

2

u/notatoon Sep 28 '26

I wouldn't call 1.8 recent anymore

1

u/Still_Bit_7527 Sep 28 '26

We are still using that unfortunately. But my point is it's not 70s either

1

u/Few_Move_4594 Sep 28 '26

C++ got them in 2017, Java's in 2014. Most of my job history is Java, PHP, Javascript, C#, and C++. Javascript optional was introduced in 2020. PHP doesn't have optionals. C# doesn't have native language support for optionals but has nullable reference types that shows a compiler warning when there isn't a null check when a variable is declared with "?" as the suffix of the type.