3 ms·
To expand on that, Crystal's Nil goes hand-in-hand with its union types. So, if you want to allow a function's argument to be either an integer or nil, then the
by mumblemumble 6y ago
To expand on that, Crystal's Nil goes hand-in-hand with its union types. So, if you want to allow a function's argument to be either an integer or nil, then the parameter type is "Int | Nil".
It's not entirely unlike how algebraic data types with a "None" case work in the ML family of languages.
- brundolf 6y agoOr TypeScript, for a more well-known example
- nielsbot 6y agoI think ad-hoc union types is one of TypeScript's best features.
- eyelidlessness 6y agoI think it would be more straightforward to say expressions (/bindings/return types etc) can be null, but not implicitly so, and must be checked for a value before attempting to use the value. Or as a sibling comment said, in TypeScript terms, `strictNullChecks`.
- KMag 6y agoIn other words, it avoids "Tony Hoare's billion dollar mistake" in conflating reference types and optional types. Tony Hoare had a gut feeling that it was going to cause code messiness when he made all references reference nullable in Algol W (instead of going through and adding extra type rules and tracking for nulls), but it was a very simple change to the compiler's type checker. It was too hard to resist such a simple change that made the language more flexible, even if it introduced footguns. In most languages without ML-like type systems, null is treated as a "bottom type", as if it were a subclass of all classes. This violates the Liskov substitution principle and results in an unsound type system. When I was working on Google's indexing system, I was very excited to hear rumors that Ken Thompson and Rob Pike were working on a new language. When it was unveiled, I was pretty disappointed to learn that it repeated Tony Hoare's billion dollar mistake.