Remix.run Logo
▲ ardit33 a day ago

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).

▲ratorx a day ago | parent | next [-]

> 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.

▲rhdunn a day ago | parent [-]

1. Have declared nullability (e.g. `String? foo` / `val foo: String?`)

2. Use null coalescing (e.g. `foo?.bar?.baz`)

Languages such as Kotlin and C# support this style o programming.

The thing that I don't like about Optional types (in e.g. Scala or Java) is that they are too verbose. For example, instead of:

    return foo?.bar?.baz ?? 0
you have something like:

    return foo.flatMap(_.bar).flatMap(_.baz).getOrElse(0);
This is even harder to read if you have more complicated logic, requiring implicit lambdas and other machinery.
▲glandium a day ago | parent | next [-]

You can mostly write the former in rust: https://rust.godbolt.org/z/5M6nr3dzs

There are caveats, such the function using ? having to return an Option, which can be worked around by using a closure.

I think try blocks will make this slightly easier.

▲kaelwd 16 hours ago | parent [-]

`(try { foo?.bar?.baz? }).unwrap_or(0)` is maybe more understandable at a glance than the closure version but still more verbose than other languages.

▲hiimkeks 18 hours ago | parent | prev | next [-]

To me, foo?.bar?.baz ?? 0 looks really hard to reason about, if I imagine it sits in the context of some wider processing code.

Explicit options nudge you towards a data model that handles the None/null case early, so you don't have to write code that does the coalescing. If you parse-dont-validate early, you more often than not end up with a data model that is simpler and easier to process.

But then, this also depends on the sort of code you write. In the code I write, matching/"if-let"ing on Options to understand their value and then translating that into some enum that encodes the value on the abstraction level of the domain or algorithm has often been really really helpful.

▲Fluorescence 16 hours ago | parent [-]

It's nice.

I'm an old developer... I recognise the instinct to resent and avoid new unfamiliar syntax when I've got something that works for me. After some resistance, I give it a go, maybe working on someone else's code that uses it... and then it's suddenly "damn, the lang designers knew what they were doing".

This is one of those. What you might miss is that nested optionality is often the cleanest accurate representation and not error states e.g. a hierarchy of capabilities as a tree of options getting progressively more fine-grained yet you need to work with fine-grained capabilities at the leaves.

Nested optionality appears to be a bad data-structure to avoid when the code it requires is verbose and gruelling. Workarounds to reshape or design for less gruelling code are anti-patterns, adding complexity not to model the problem but to solve a language design weakness, which this solves.

▲tcfhgj a day ago | parent | prev [-]

Both variants are just different syntax for the same thing.

▲nine_k 21 hours ago | parent | next [-]

The second is twice as long as the fist. The difference matters.

▲ 21 hours ago | parent | prev [-]
[deleted]
▲bunderbunder a day ago | parent | prev | next [-]

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 nulls 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 a day ago | parent | prev | next [-]

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 a day ago | parent | prev | next [-]

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

▲zeratax 18 hours ago | parent | prev | next [-]

I feel like youre trying to replace one sentinel value with another? Now instead of doing NULL checks I do empty string checks or look for that starter initial value. Honestly it seems worse than NULL cos now I can't even represent an empty string

▲vrmiguel 21 hours ago | parent | prev [-]

> Usually done with the empty "" string

That seems so much worse than a `Option<String>`