logoalt Hacker News

tialaramexlast Tuesday at 6:52 PM5 repliesview on HN

I don't see how Try (the ? operator) is hidden control flow. It's terse, but it's not hidden.


Replies

9rxlast Tuesday at 8:04 PM

Even the laziest programmer is going to want to wrap the error with another error, and usually you will want to do more than that.

You can put that in-band, with something like:

    v := funcWithError()? err := { 
        return fmt.Errorf("lazy programmer wrapping: %w", err)
    }
But in that case what have you really gained?

Or you can do some kind of magic to allow it to happen out of band:

    // found somewhere else in the codebase
    func wrapErr(err error) error {
        if err == nil {
            return nil
        }
        return fmt.Errorf("lazy programmer wrapping: %w", err)
    }

    v := funcWithError()?(wrapErr)
        
But that's where you start to see things hidden.
ok_dadlast Tuesday at 7:18 PM

I personally agree, but I’m not the go team. The hidden control flow was specifically called out but about the try keyword. I like the ? and similar ways of checking nulls, but personally I don’t mind the verbosity in go, even if there are footguns.

jchwlast Tuesday at 7:25 PM

IMO: because it behaves like structured control flow (i.e. there is a branch) but it doesn't look like structured control flow (i.e. it doesn't look like there is a branch; no curly braces). I don't think there's a single other case in the Go programming language: it doesn't even have the conditional ternary operator, for example.

show 2 replies
skywhopperlast Tuesday at 10:56 PM

That only covers one tiny case among several possible error flows. Why add special syntax for that?

kiitoslast Tuesday at 8:39 PM

Currently if you want to return from a function/method you need to type `return` in the source code. And return is a single expr, it can't be chained or embedded, and in almost all cases it exists on its own line in the file. This is an important invariant for Go, even if you don't understand or buy its motivation. `?` would fundamentally change that core property of control flow. In short, chaining is not considered to be a virtue, and is not something that's desired.