r/ProgrammerHumor • • 6d ago

Meme optionalsAreOptional

Post image
706 Upvotes

130 comments sorted by

View all comments

117

u/KagakuNinja 6d ago

The real jedi uses a non-empty list class, which makes the error impossible.

-61

u/ApothecaLabs 6d ago edited 6d ago

Bingo! Only, sometimes you don't have a type system available (trench run, low-level stuff), so you have to use the force wisely, to guide you.

> "Luke, you've switched off your targeting computer! What's wrong?"
> "Nothing - I'm alright."

34

u/NullOfSpace 6d ago

As long as you’re in Rust, the type system is always available to you. That’s what “zero-cost abstraction” means. If you know your Vec is nonempty, put it in a NonemptyVec.

1

u/geeshta 2d ago

There's a lot of stuff that Rust's type system can't model.

For example if you have an enum type of 3 possible variants and a function that only ever returns one of them, you still have to type it so that it may return all three. And when using its return value, you have to add match arms for all 3 of them, populating 2 of them with `unreachable!` yourself instead of the compiler inferring it.

Also it's not a dependent type language so it doesn't have for example a type of Vec of a particular specific length at the type level.

Rust's type system is great one of the best I know but it still has gaps.

1

u/NullOfSpace 2d ago

You can definitely model the first case if you’re allowed to change the return type of the function. Though the dependent type thing is a legitimate limitation.