Skip to content

Proposal: Go2: try: Change try() proposal to use a special character in the position where an error is returned #32884

Closed
@integrii

Description

@integrii

It looks like #32437 is the current most popular error handling proposal. This proposal wishes to introduce a try() statement that returns the default values of all return variables if a value other than nil is returned as the last value from a function. In normal function, the try() statement returns all values that are not the final error value.

How about instead of wrapping things in parentheses all over the place, we directly put a special key (like !! or anything else) into the place where the error is returned? That's shorter and all other behaviour is the same. Also, you don't have invisible returns here, or require the error to be the last thing returned.

This also discourages changing try statements together which keeps code more readable.

func Foo() (err error) {
        file, !! = os.Open("file1.txt")
        defer file.Close()
	parsed, !! = parseFile(file)
	fmt.Printf("%s", parsed)
	return nil
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions