623
know the features of your language
(lemmy.world)
Post funny things about programming here! (Or just rant about your favourite programming language.)
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