Skip to content

proposal: Go 2: handle statement to use a type switch expression #28344

Closed
@nilslice

Description

@nilslice

I would like to see if a modification to the proposed error handling design would be possible or is of any interest to other Go programmers. Just as switch can be parsed as a *ast.SwitchStmt as well as a *ast.TypeSwitchStmt, I suggest that the same hold true for the handle statement.

For example, the handle could optionally support:

func a() error {
    handle e := err.(type) {
    case *mypkg.ErrSpec:
        // log, react, etc
        return errors.Newf("spec error (%d): %v", e.SpecID, err)
    default:
       return err
    }

    check mypkg.SpecFunc()
    return nil
}

I think this would encourage more Go users to create useful error implementations, result in simpler debugging, and make it easier for programmers to write more reliable code.

It's clear that a type switch could be used inside the handle block, but I think the ergonomics of lifting the syntax up to the handle level work better, and make it more clear. If both a type switch and simpler error handling semantics are needed in the handle block, the user could simply use the handle statement as originally proposed, supporting both styles.

Apologies if this has already been proposed. I didn't find a duplicate suggested here or in the wiki.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeLanguageChangeSuggested changes to the Go languageProposalerror-handlingLanguage & library change proposals that are about error handling.v2An incompatible library change

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions