4 ms·
null itself isn’t the billion dollar mistake, it’s that literally every object can be T|null. Undefined or null is actually quite useful, as long as you’re expe
by chomp 4y ago
null itself isn’t the billion dollar mistake, it’s that literally every object can be T|null. Undefined or null is actually quite useful, as long as you’re expecting it.
- disintegore 4y agoNull safety means you can't dereference a null pointer. Therefore something like `T | null` is not possible, because `null` doesn't exist. What you'd have instead is an Option or Maybe type and a compiler that requires exhaustive handling of it.
- masklinn 4y agoIn the informal context of GP’s comment, null is a singleton type whose sole value is null. That exact syntax (modulo langage divergences) is one of the options for “nullable” in Python: https://docs.python.org/3/library/typing.html#typing.Optional https://docs.python.org/3/library/typing.html#typing.Optiona...
- disintegore 4y agoWhich can still lead to unrecoverable runtime errors, that could otherwise be compilation errors.
- masklinn 4y agoWhat are you talking about?
- disintegore 4y agoI'm talking about the fact that nullable types do not on their own guarantee null safety and that these two things are not equivalent.
- masklinn 4y agoThat's about as unclear as your previous comment, at best. At worst, it's just wrong. The typechecker would only allow on the union operations which are allowed on both types, anything beyond that would first have to use type checks or assertions in order to split the union. So, again, what are you talking about?
- disintegore 4y agoThat's actually a good point. I made assumptions about the meaning of the term `null` that can't be true in every language that uses it. There are some exceptions to the behaviour you describe, like for instance with C# which for the longest time only allowed value types to be annotated as nullable, and only very recently extended this to reference types, and only as an opt-in feature, and the type checker only throws warnings, etc. That would be a case of a language which is not "null safe" but provides faculties for accomplishing that.
- Tade0 4y agoSeems like an easy fix. At least in languages like TypeScript: https://www.typescriptlang.org/tsconfig#strictNullChecks https://www.typescriptlang.org/tsconfig#strictNullChecks
- KMag 4y agoYes, null safety is a very simple change to type systems and type checkers. Unfortunately, there's a lot of cultural momentum in this regard.
- pizlonator 4y agoThat’s not necessarily a mistake. Claiming that a reference cannot be null means that if you later realize it has to be, you’ve got a potentially compat-breaking refactoring to make. So, nullable-by-default is either a good idea or a bad idea depending on lots of complex reasons.
- Lewton 4y agoThat's the exact same issue you run into if you suddenly find out that you need to use a different type (because.. Yeah, that's exactly what it means). That doesn't mean you should default to writing all your code without types