Hacker News new | ask | show | jobs
by floating-io 500 days ago
The biggest issue I have with this proposal is that reading the code in naïve fashion comes up with the wrong answer for me; YMMV. The proposed form--

  foo ? { bar }
Reads naturally to me as "If foo then bar", when it's actually "If foo's error return exists then bar". I would suggest a different operator character, because this one reads wrongly IMO.

Maybe it's just because I originally come from C, where `foo ? bar : baz` meant "if foo then bar else baz", but the fact remains...

6 comments

That is exactly how go usually works with error handling though.

    func mayfail() error {
        if snafu {
            return errors.New("oops")
        } else { return nil}
    }


    err := mayfail()
    if err != nil { handle }

Same as `mayfail() ? handle : happypath` would behave with lazy evaluation.
Go doesn't have a ? operator today, and the ? operator being used for error handling has precedence in Rust and Zig, so it doesn't seem to be all that out of the ordinary or without precedent in other languages.
The proposed behavior is the opposite of how Swift and Rust use ?. `foo?.bar()` invokes `foo.bar()` in the non-error case, while in Go `foo ? { bar() }` invokes it in the error case.

  foo else { bar }
How about this? :)

    { foo } catch { bar }
This is probably the one that would anger Golangers the most lol
They could even have typed error checking for different errors and an automatic return error syntax.

    { Foo }
    catch (err) {}
    catch (err2){}
    throw
Agreed. The reduction in boilerplate to route error conditions is what we really need, if we are going to add any special error concept. errors.Is/errors.As and the accompanying switch/if statements can quickly consume entire screens of code for all but the most simplest of failure cases and as a result it often sees developers become lazy and just return err instead.
It could be:

foo ?! { bar }

But, now we’re potentially confusing the “negation” and the “exclamation” meanings the bang symbol usually tries to communicate.

I tend to agree that ? looks like "if then" when what we really want is some sort of coalescing, or "if not then".

foo ?? { bar }

foo ?/ { bar }

foo ?: { bar }

foo ?> { bar }

foo ||> { bar }

Im not sure I like the idea at all though. It seems like a hack around a pretty explicit design choice. Although I do tend to agree the error handling boilerplate is kind of annoying.

V, which is syntactically similar to Go, uses

  foo or { bar }