Remix.run Logo
▲ ratorx 2 hours ago

> 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 an hour 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 25 minutes 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.

▲tcfhgj an hour ago | parent | prev [-]

Both variants are just different syntax for the same thing.