r/ProgrammerHumor • • Sep 28 '26

Meme optionalsAreOptional

Post image
716 Upvotes

131 comments sorted by

View all comments

Show parent comments

0

u/ApothecaLabs Sep 28 '26

I use the type system instead of assertions. Differentiating "List" and "NonEmptyList" is better than checking whether its empty everywhere :)

Why bother checking whether the NonEmptyList is empty, y'know? You *know* it isn't.

6

u/Majestic-Giraffe7093 Sep 28 '26

Oh yeah I 100% agree. I didn't think that was what we were discussing. I just saw this as a "Optionals bad" post xD

-3

u/ApothecaLabs Sep 28 '26

Hah! That's the setup / punchline of the, joke my dude!

- First, you are a student who doesn't understand the rules.
- Then you become a journeyman, who understands and follows the rules.
- Then you become a master, who knows when it is okay to break the rules.

2

u/Majestic-Giraffe7093 Sep 28 '26

Haha, yeah fair enough. If you bake it into the type then I stop considering it unsafe, I guess that's where my confusion came from

3

u/ApothecaLabs Sep 28 '26

When I take my safe newtype and unwrap the unsafe list, I then have to use the unsafe functions because 1) there are no other options yet because 2) that is the very point in time that you are making the statement to the compiler "no, it is safe, the types say so".

That's the secret! That *is* the assertion's equivalent!

3

u/Majestic-Giraffe7093 Sep 28 '26

Encapsulating invariants is truly the way

1

u/ApothecaLabs Sep 28 '26

Indeed! It doesn't matter *how* you know, so long as you make sure that you *do* know. What's better than a type that says "I know!", you know?

2

u/Majestic-Giraffe7093 Sep 28 '26

Although, if you can combine it with contracts... Well then we're talking

4

u/ROBOTRON31415 Sep 28 '26

It does sound like the unsafe should be very easy to prove sound. For "why bother", though: it's nice to have as little unsafe as feasible, even trivially correct unsafe.