3 ms·
Sibling comments mentioned Option types, which are assumed the currently best approach. I agree, but would like to add Python as a different example. Null hand
by diarrhea 3y ago
Sibling comments mentioned Option types, which are assumed the currently best approach. I agree, but would like to add Python as a different example.
Null handling in Python is actually sane, despite there being no Option type.
Its Null is called None, which is of type NoneType, which sits at the very top of the type hierarchy (inherits from object). That’s it. Everything else is separate from it. You perform a check for None, and after passing, you are guaranteed no null reference. Dereferencing cannot blow up.
A variable of type “Union of None and SomeClass” is safe to use after a None check. Type checkers will flag missing checks as errors. This is in contrast to C# or Java, where any reference type can end up in a rug pull and cause a null dereference.
In Python, assuming a type checker, checks are mandatory and cannot be forgotten (quite similar to Option types actually). In the other languages they’re not mandatory and can be forgotten (by default).
- mirashii 3y agoThere's actually still plenty of insane things about None in Python. https://peps.python.org/pep-0661/ https://peps.python.org/pep-0661/ is a great example of that. > Sibling comments mentioned Option types, which are assumed the currently best approach Option types are actually just the tip of the iceberg though. The real power is in languages that support algebraic data types. Option is just one common example, used to distinguish between just Some and None, but it's reasonable to have an enum with other states, each optionally with its own associated data.