logoalt Hacker News

rhdunn • yesterday at 11:19 PM • 2 replies • view on HN

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.

Replies

glandium • today at 12:19 AM

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 • yesterday at 11:37 PM

Both variants are just different syntax for the same thing.