10 ms·
So how do you express the idea of null when the language doesn't have null?
by hzhou321 8y ago
So how do you express the idea of null when the language doesn't have null?
- greydius 8y agodata Maybe a = Nothing | Just a
- srgpqt 8y agoSum types. Option<T> types which can have either Some(value) or Nothing for values. Statically compiled languages can typecheck this so you don’t have null pointer errors. Some languages like TypeScript have this baked into the language syntax, so you can do: let x: Foo | null = bar; and is also properly type checked.
- DonHopkins 8y agoYou could pass a function that never returns. ;)
- tybit 8y agoThe problem isn’t actually null perse, it’s an implicit null. E.g. it’s perfectly fine to say you might have an empty value via ‘Foo | null’ or ‘Foo?’ or with something like null, ‘Maybe Foo’ but to sneak it into plain old ‘Foo’ is problematic (a billion dollars worth of problems).
- brazzy 8y agoI'd say the real problem is having nullability as the default for all types.
- Iv 8y agoYou don't. That's the point. Look at how C++ references (not pointers) work. You can't declare one without giving it a valid object to reference. The only reason why you may need null is to check whether a reference is null. You remove this need by forbidding it in well defined programs.
- dragonwriter 8y agoDepends on the other focuses of the language. Option/Maybe monads are one option popular in static functional languages. For set oriented languages, an empty set is an option. (This is very similar to the previous option, actually.) Null is kind of a middle ground between these options and a ad hoc use of a specific sentinel value like using a signed integer variable when the data is logically unsigned, so you can use “-1” for missing data.
- hzhou321 8y agoI see. So the idea of NULL is the idea of sentinel value. And to express the idea of sentinel value is to create a wrapper around the value and have the wrapper always check for sentinel by default. Is this interpretation correct? So I guess the better question is how do you express the idea of skipping checking for Sentinel when the language always checks for sentinel?
- matt_kantor 8y agoWhat do you want the runtime behavior to be if the sentinel value is produced?