4 ms·
> No Nulls Does it not? It has 'nil', just like Ruby. Basically the same thing, the docs literally say it's 'similar to null in other languages'. https://crys
by benbristow 6y ago
> No Nulls
Does it not? It has 'nil', just like Ruby. Basically the same thing, the docs literally say it's 'similar to null in other languages'.
https://crystal-lang.org/reference/syntax_and_semantics/literals/nil.html https://crystal-lang.org/reference/syntax_and_semantics/lite...
- TylerE 6y agoIt has a Nil type, not a Nil Value. So a function might return an Int or a Nil, but if you then try to, say, invoke a function expecting an Int on a Nil, it predictably blows up. An Int cannot be Nil itself.
- mumblemumble 6y agoTo 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.
- Shoop 6y agoIt looks like it has nullable (nilable) types [0]. So a variable cannot be nil unless it is explicitly marked as having a nilable type. [0] https://crystal-lang.org/reference/syntax_and_semantics/type_grammar.html https://crystal-lang.org/reference/syntax_and_semantics/type...
- straight-shoota 6y agoMore accurately would be "no surprising nulls at runtime". If a value can be `nil`, the compiler forces to treat it as nilable. It doesn't throw random null pointer exceptions.