IssueRaiser

proposal: spec: reduce noise in return statements that contain mostly zero values

Update: the current proposal is to permit `return ..., v` to return the zero value for all but the last result, and to return `v` as the last result. The most common use will be `return ..., err`. A variant under discussion include `return ..., v1, v2` to return zeroes for all but the last N. Another variant is to permit `return ...` to return zeroes for all. In general `...` is only permitted if it omits one or more values--`func F() err { return ..., errors.New("") }` is not permitted. --- # Proposal In return statements, allow `...` to signify, roughly, "and everything else is the zero value". It can replace one or more zero values. This is best described by example: Given the function signature `func() (int, string, *T, Struct, error)`: `return 0, "", nil, Struct{}, nil` may be written `return ...` `return 0, "", nil, Struct{}, err` may be written `return ..., err` `return 0, "", nil, Struct{X: Y}, nil` may be written `return ..., Struct{X: Y}, nil` `return 1, "", nil, Struct{}, nil` may be written `return 1, ...` `return 1, "a", nil, Struct{}, nil` may be written `return 1, "a", ...` `return 1, "", nil, Struct{}, err` may be written `return 1, ..., err` `return 1, "a", nil, Struct{X: Y}, err` may be written `return 1, "a", ..., Struct{X: Y}, err` The following is invalid: `return ..., Struct{X: Y}, ...` — there can be at most one `...` in a return statement # Rationale It is common for a function with multiple return values to return only one non-zer

Posted by jimmyfrasche on Jul 26, 2017, 9:36 PM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.4,500 issues0 comments

github (Other)

proposal: spec: reduce noise in return statements that contain mostly zero values

jimmyfrascheJul 26, 2017, 9:36 PM

Update: the current proposal is to permit `return ..., v` to return the zero value for all but the last result, and to return `v` as the last result. The most common use will be `return ..., err`. A variant under discussion include `return ..., v1, v2` to return zeroes for all but the last N. Another variant is to permit `return ...` to return zeroes for all. In general `...` is only permitted if it omits one or more values--`func F() err { return ..., errors.New("") }` is not permitted. --- # Proposal In return statements, allow `...` to signify, roughly, "and everything else is the zero value". It can replace one or more zero values. This is best described by example: Given the function signature `func() (int, string, *T, Struct, error)`: `return 0, "", nil, Struct{}, nil` may be written `return ...` `return 0, "", nil, Struct{}, err` may be written `return ..., err` `return 0, "", nil, Struct{X: Y}, nil` may be written `return ..., Struct{X: Y}, nil` `return 1, "", nil, Struct{}, nil` may be written `return 1, ...` `return 1, "a", nil, Struct{}, nil` may be written `return 1, "a", ...` `return 1, "", nil, Struct{}, err` may be written `return 1, ..., err` `return 1, "a", nil, Struct{X: Y}, err` may be written `return 1, "a", ..., Struct{X: Y}, err` The following is invalid: `return ..., Struct{X: Y}, ...` — there can be at most one `...` in a return statement # Rationale It is common for a function with multiple return values to return only one non-zer

Comments (0)

No comments yet. Start the discussion.

Comment on this issue