| ▲ | rhdunn a day ago | |||||||||||||
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:
you have something like:
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. | ||||||||||||||
| ||||||||||||||
| ▲ | 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. | ||||||||||||||
| ||||||||||||||
| ▲ | tcfhgj a day ago | parent | prev [-] | |||||||||||||
Both variants are just different syntax for the same thing. | ||||||||||||||
| ||||||||||||||