I started new topic, but I guess I can say few words here. Note that majority of my experience comes from backend development, so I might have a bit different view from others.
I’m new to ReScript, but coming from other languages that utilize Option/Maybe type I find it hard to use due to missing actually convient syntax for unwrapping option types inside functions. I feel like I wrote implementation of Option in JavaScript directly, but it’s even harder to use.
Rust uses ? - Rust By Example with a caveat that the function using ?
operator becomes Result/Option automatically - which makes perfect sense. In that scenario ?
works like a short-circuit to a function.
Haskell’s do
notation is similar to Rust’s ?
but it is more powerful (as it handles all monadic types), at a cost of introducing “alternative” syntax to a function (Rust uses the same syntax, just introduces an operator).
All things considered I think Rust approach is superior here - albeit limited to few use cases, those use cases are contributing to all/almost all of potential use cases in ReScript. Rust developers are using ?
operator extensively.
Does it make you “ignore” errors? To some degree, yes. But after all code is properly failing. When it fails you might be asking yourself “why”. Then you go to the codebase, see bunch of ?
and say: “one of them failed”. If you wish you might have more explicit handling: you might remap your Option to Result to have explicit error handling, and continue using ?
syntax. Having explicit errors is a reason why ?
is much more common with Result type, and not the Option type.
I personally don’t buy the argument I’ve heard multiple times “not having a syntax makes me think about error handling”. From my experience in most cases you want to handle errors, but at the same time, don’t really care too much about them. In most cases errors are not part of a business logic. Thus errors should be non-obtrusive, thus any syntax sugar is welcomed. And usually, Option means “there is nothing more to do”, like you are looking for an item in an array, there is not item, you cannot proceed further, and if that’s correct behavior that the item is not there, you just quit the processing.
The syntax sugar is optional. Language is a tool, not a way of life - that should be left to particular developer’s/team’s opinion.
Furthermore, ?
behaves a bit like a controlled throw in a sense that it interrupts further processing. And some languages actually use throwing extensively. Not talking about >handling< part here, but it just tells, that “early quitting due to not having what program needs” is a pattern common-enough and useful-enough, that having a syntax for it makes sense.