this post was submitted on 08 Dec 2023
622 points (96.4% liked)
Programmer Humor
32561 readers
391 users here now
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.
founded 5 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
Who said anything about panicking the minute we encounter incomplete data? Just do what Rust does and, instead of having all types be able to be null, statically enforce that all variables have an initialized value and have a value have a separate type
Option<T>
which can either beSome(T)
orNone
, and have the compiler not let you access the value inside unless you write code to handle theNone
case. There are standard library helper functions for common operations like null coalescing and, as you say, panicking when you encounter a null, but you have to explicitly tell the compiler you want to do that by callingmyOption.unwrap()
What makes this really cool is that you can have an Option<Option<T>> where
Some(None)
is not the same asNone
, so an iterator that signals end of list by returningNone
can haveNone
elements in it.Say what you will about the functional programming people but they were spot on with this one. Having an Option monad in place of the ability for null is absolutely the way to go. I'd say it's the future but Lisp and APL had this figured out in the 60s