logoalt Hacker News

kbolinolast Tuesday at 5:59 PM0 repliesview on HN

It's an interesting idea. Right now, you can do something like this:

    res := someFunc() // func() any
    switch v := res.(type) {
    case error:
        // handle error
    case T:
        // handle result
    default:
        panic("unexpected type!")
    }
Then, presumably, a T|error sum type would be a specialization of the any type that would allow you to safely eliminate the default arm of the switch statement (or so I would like to think -- but the zero value issue rears its ugly head here too). Personally, I'd also like to see a refinement of type switches, to allow different variable names for each arm, resulting in something like the following hypothetical syntax:

    switch someFunc().(type) {
    case err := error:
        // handle error
    case res := T:
        // handle result
    }
However, there's no real syntactic benefit for error handling to be found here. I like it (I want discriminated unions too), but it's really tangential to the problem. I'd honestly prefer it more for other purposes than errors.