logoalt Hacker News

ardit33 • yesterday at 10:16 PM • 4 replies • view on HN

Null should not exist, and Optionals are an anoying workaround/hack to it. Old languages like Tcl eliminated it, (every object has an initial value, it is never null).

It creates an interesting problem, as sometimes you want to know the absence of value. Usually done with the empty "" string.

The other way to handle is that every Object is initiated (pointing) to an starter initial value. (prototype based languages).


Replies

ratorx • yesterday at 10:30 PM

> Optionals are an annoying workaround/hack to it

Sometimes you do want to represent the concept of “this whole datatype OR nothing”. How do you suggest this is modelled? I think optionality is pretty fundamental. What’s wrong about classical null is that it ascribes optionality to all types regardless of whether they need it.

Being forced to represent optionality with a “special” default is terrible design IMO - see the recent SSH RCE on Musl, largely due to the poor choice of indicating “crypt failed”. Default values can be quite dangerous. Sometimes the domain you are modelling supports such a value, but having “default value” be the default (and not a choice) is a similar evil to default null.

➕ show 1 reply
bunderbunder • today at 12:13 AM

Empty string is not a valid alternative to null because the empty string is still a string. There are valid cases where “this value is not set” and “this value is set to an empty string” mean two different things.

There are even more valid cases where “this integer is not set” and “this integer is set to 0” are different things.

It’s also a type safety concern. Using sentinel values is comparable to the “billion dollar mistake” version of ills because it creates a special value that implicitly exists and is not part of the variable’s type definition. This means the compiler can’t help you remember to check it. You have to just remember, and forgetting is a bug. With optionals (including opt-in nullability like in Kotlin), on the other hand, the compiler knows when you do and do not have to worry about a value not being present, and can raise a compile time type error when you forget.

setr • yesterday at 10:29 PM

I don't see how initial values solves for much, or how an Optional constitutes a workaround; if anything, I see initial/null values being a hack to get around Optional, which is an explicit declaration of what possibilities exist.

Initial values, like null, is an attempt to smuggle the "unset" case into the type without declaring it. And like null, the defensively written logic should prefix every single usage with a check for validity.

Optional is just a natural subset of the generalization that multiple possible states exist; in this case, just the two, and sum types to properly handle bigger sets.

Initial/null never admit more than 2 possible states, and also believe everything is always in one of those 2 states.

rcxdude • yesterday at 11:10 PM

"Always has a value" just creates an in-band signaling problem, where the "absense of value" overlaps with the valid values.