go icon indicating copy to clipboard operation
go copied to clipboard

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

Open jimmyfrasche opened this issue 7 years ago • 55 comments

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-zero result when returning early due to errors.

This creates several annoyances of varying degrees.

When writing the code one or more zero values must be manually specified. This is at best a minor annoyance and not worth a language change.

Editing the code after changing the type of, removing one of, or adding another return value is quite annoying but the compiler is fast enough and helpful enough to largely mitigate this.

For both of the above external tooling can help: https://github.com/sqs/goreturns

However, the unsolved problem and motivation for the proposal is that it is quite annoying to read code like this. When reading return 0, "", nil, Struct{}, err unnecessary time is spent pattern matching the majority of the return values with the various zero value forms. The only signal, err, is pushed off to the side. The same intent is coded more explicitly and more directly with return ..., err. Additionally, the previous two minor annoyances go away with this more explicit form.

History

This is a generalized version of a suggestion made by @nigeltao in https://github.com/golang/go/issues/19642#issuecomment-288559297 where #19642 was a proposal to allow a single token, _, to be sugar for the zero value of any type.

I revived the notion in https://github.com/golang/go/issues/21161#issuecomment-317891763 where #21161 is the currently latest proposal to simplify the if err != nil { return err } boilerplate.

Discussion

This can be handled entirely with the naked return, but that has greater readability issues, can lead too easily to returning the wrong or partially constructed values, and is generally (and correctly) frowned upon in all but the simplest of cases.

Having a universal zero value, like _ reduces the need to recognize individual entries as a zero value greatly improving the readability, but is still somewhat noisy as it must encode n zero values in the common case of return _, _, _, _, err. It is a more general proposal but, outside of returns, the use cases for a universal zero value largely only help with the case of a non-pointer struct literal. I believe the correct way to deal that is to increase the contexts in which the type of a struct literal may be elided as described in #12854

In https://github.com/golang/go/issues/19642#issuecomment-288644162 @rogpeppe suggested the following workaround:

func f() (int, string, *T, Struct, error) {
  fail := func(err error) (int, string, *T, Struct, error) {
    return 0, "", nil, Struct{}, err
  }
  // ...
  if err != nil {
    return fail(err)
  }
  // ...
}

This has the benefit of introducing nothing new to the language. It reduces the annoyances caused by writing and editing the return values by creating a single place to write/edit the return values. It helps a lot with the reading but still has some boilerplate to read and take in. However, this pattern could be sufficient.

This proposal would complicate the grammar for the return statement and hence the go/ast package so it is not backwards compatible in the strict Go 1 sense, but as the construction is currently illegal and undefined it is compatible in the the Go 2 sense.

Possible restrictions

Only allow a single value other than ....

Only allow it on the left (return ..., err).

Do not allow it in the middle (return 1, ..., err).

While these restrictions are likely how it would be used in practice anyway, I don't see a need for the limitations. If it proves troublesome in practice it could be flagged by a linter.

Possible generalizations

Allow it to replace zero or more values making the below legal:

func f() error {
  // ...
  if err != nil {
    return ..., err
  }
  // ...
}

This would allow easier writing and editing but hurt the readability by implying that there were other possible returns. It would also make this non-sequitur legal: func noop() { return ... }. It does not seem worth it.

Allow ... in assignments. This would allow resetting of many variables to zero at once like a, b, c = ... (but not var a, b, c = ... or a, b, c := ... as their is no type to deduce) . In this case I believe the explicitness of the zero values is more a boon than an impediment. This is also far less common in actual code than a return returning multiple zero values.

jimmyfrasche avatar Jul 26 '17 21:07 jimmyfrasche

Why not named returns? func f() (i int, ss string, t *T, s Struct, err error) {}

OneOfOne avatar Jul 27 '17 15:07 OneOfOne

I like this idea, but I can't think of any place where I'd use it for anything other than filling in all but the last value. Given that, I think one could reasonably make it a little less general and allow only this form (allowing any expression instead of err, naturally)

return ...err

Note the lack of comma. I'm not sure whether it's better with a space before "err" or not.

rogpeppe avatar Jul 27 '17 16:07 rogpeppe

@OneOfOne Naked returns are fine for very short functions but are harder to scale: it gets too easy to accidentally return partially constructed values or the wrong err because of shadowing. Other than that, or maybe because of that, I like the explicit syntax better. A naked return says "go up to the function signature to see what can be returned, then trace through the code to see what actually gets returned here" whereas return ..., err says "I'm returning a bunch of zero values and the value bound to the err in scope"

@rogpeppe that was the original syntax proposed that I based this proposal off of. I don't like it because it appears to be a spread operator common in dynamic languages so it's a bit confusing. Having the comma makes it superficially more similar to "⋯" in mathematical writing and with like purpose. I agree that this would almost always be used as return ..., last where last is a bool or error and sometimes as a return ..., but I don't see any particular reason to artificially limit it to that. If it's not very useful it will not get used often. Is there any particular concern that a line like return 7, ... would be more confusing or error prone than a return ..., true?

jimmyfrasche avatar Jul 27 '17 17:07 jimmyfrasche

@jimmyfrasche I just don't see that it would ever be used, and given that, the comma seems like unnecessary overhead for what would be a very commonly used idiom.

How many places in existing code can you find where eliding all but the first argument (or all but several arguments) would be useful?

rogpeppe avatar Jul 27 '17 17:07 rogpeppe

If we permit both ..., v and v, ... how do we justify the exclusion of ..., v, ...? Except for that fact that it is impossible to implement?

ianlancetaylor avatar Jul 27 '17 18:07 ianlancetaylor

@rogpeppe return ...aSlice looks too much like https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator That's not a strong argument against it, but I'd still rather not see it. While I would be surprised if this proposal is accepted in any form, I certainly wouldn't be mad if that variant was the one accepted, as it is the most common case. I really don't see any justification for the limitation, personally. It would be interesting to run an analysis on go-corpus to see if there are any. I don't have the time to do that for a bit, so anyone can feel free to beat me to it.

@ianlancetaylor that's a fine point. Impossible to implement is justification enough for me. Though it would be possible to implement in some cases, where v is of a type returned only once, but then subtle and distance changes could make it suddenly ambiguous. I would note that the other two existing uses of ... are also "at most once". You can't do func(a ...string, b ...int) or append(bs, s1..., s2...) even though the first is only sometimes unambiguous and the latter would be merely inefficient (or rather non-obviously inefficient).

Another option, that I'm fairly sure is a bad idea, would be to allow "keyed returns" to work in conjunction with named returns, by analogy with keyed struct literals:

func() (a int, b string, err error) {
  // ...
  if err != nil {
    return err: err
  }
  // ...
  return b: name, a: -x // I purposely flipped the order here
}

though that would interact poorly with the semantics of the naked return. If a is non-zero what happens when I write return err: err? The answer should clearly be that only err is returned, but it's still a potential source of confusion.

jimmyfrasche avatar Jul 27 '17 19:07 jimmyfrasche

Counter-proposal that has been suggested elsewhere in the past (https://github.com/golang/go/issues/19642). Allow _ to be used as the zero value of any type in expressions. Currently _ in var _ T = foo may be thought of as foo > /dev/null (from Effective Go). Similarly, _ in var foo T = _ could be thought of as foo < /dev/null. Then _ could be used in return statements.

E.g. 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 return _, _, _, _, err

return 0, "", nil, Struct{X: Y}, nil may be written return _, _, _, Struct{X: Y}, _

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

mewmew avatar Aug 12 '17 21:08 mewmew

When your functions has so many return values that typing them becomes a chore, that's a sign that you need to redesign your function, not the language.

davecheney avatar Aug 12 '17 21:08 davecheney

When your functions has so many return values that typing them becomes a chore, that's a sign that you need to redesign your function, not the language.

I think the example was given simply to provide a one of each function signature that is useful as a showcase. While not likely to see so many return values in real world code, returning the zero value of a struct using _ rather than image.Rectangle{} may improve readability; at least that's the idea of the proposal.

mewmew avatar Aug 12 '17 21:08 mewmew

@davecheney indeed.

My argument is that the primary benefit of the succinct syntax is that it improves the readability. If it makes it easier to type that's just a bonus.

If you see return ..., err you don't need to think about what else is being returned.

You don't need to double check for things that are suspiciously close to a zero value like return Struct{''}, err or return O, err or return nil, err (when nil has been shadowed for some reason).

It's immediately obvious that the only relevant value is err. Idioms such as

if err != nil {
  return ..., err
}

can be pattern matched by your brain as a unit without having to actually inspect anything. I'm sure we all do that now with similar blocks that contain one or more zero value-like expression. It's bitten me once or twice when I was debugging and my eye glazed over something that looked too close to a zero value making it hard to spot the obvious problem (I of course do not admit publicly to being the person who shadowed nil . . .).

I'm fine with how it is, however. This is just a potential way to make it a little bit easier.

@mewmew yes this proposal is based on a comment from that proposal (see the History section). I don't particularly see the point of the generic zero value except in the case of returns. It would solve the same problem, of course.

(I would like to be able to use {} as the zero value of any struct when its type can be deduced from the rest of the statement.)

jimmyfrasche avatar Aug 12 '17 22:08 jimmyfrasche

@davecheney sometimes it's not the number of return values, it's their struct-ness. Typing return image.Rectangle{}, err can be a chore. The image.Rectangle{} is the longest but also least important part of the line.

That said, this particular proposal is not the only way to sooth that chore, as the OP noted.

nigeltao avatar Aug 20 '17 02:08 nigeltao

Reopening per discussion in #33152.

ianlancetaylor avatar Oct 09 '19 00:10 ianlancetaylor

I am in favor of allowing only the return ..., x form. If other forms are good, they could be added later.

The main benefit of this for me is that it would let me use a dumb macro to expand ife into

if err != nil {
   return ..., err
}

Yes, a sufficiently smart IDE macro could look at the function return arguments to fill those in for me, but why not just simplify it so only the important information is emphasized?

earthboundkid avatar Oct 09 '19 15:10 earthboundkid

What about letting return take 1 thing regardless of how many results the func has, as long as the assignment is unambiguous to exactly 1 of the return types?

So:

    func foo() (*T, error) {
        if fail {
             return errors.New("foo")
        }
        return new(T)       
    }

bradfitz avatar Oct 15 '19 21:10 bradfitz

Does anybody see any problems with this language change?

We think we should consider just the simple case: return ..., val1, val2 where val1 and val2 become the final results of the function. Typically, of course, this would be just return ..., err. The other cases don't seem to arise enough to worry about. This would only be permitted if there are other results; it could not be used in a function that returns only error. The omitted results would be set to the zero value, even if the result parameters are named and currently set to some non-zero value.

-- for @golang/proposal-review

ianlancetaylor avatar Oct 15 '19 21:10 ianlancetaylor

@bradfitz that seems like it could cause too much fun when the return signature changes in a long func or one of the return types now satisfies an interface. It would also be unusable with interface{}, though that's a bit niche.

jimmyfrasche avatar Oct 15 '19 21:10 jimmyfrasche

It would also be unusable with interface{}, though that's a bit niche.

That's a feature.

bradfitz avatar Oct 15 '19 22:10 bradfitz

Is this legal?

var err e
func f() error {
   return ..., e
}

Is this?

func f() {
   return ...
}

Is this?

func f() int {
   return ...
}

robpike avatar Oct 20 '19 23:10 robpike

It seems confusing to have ... stand for nothing. I would require ... to stand for one or more zero values. So in your examples above, I'd only consider the last one permissible.

griesemer avatar Oct 21 '19 00:10 griesemer

There’s a typo in the first example but otherwise I would permit all three.

earthboundkid avatar Oct 21 '19 11:10 earthboundkid

I'd reject all three and only allow return ..., something (when there are 2+ return values).

ghost avatar Oct 22 '19 01:10 ghost

The ... in func Printf(format string, a ...interface{}) means zero or more.

nigeltao avatar Oct 22 '19 06:10 nigeltao

The ... in func Printf(format string, a ...interface{}) means zero or more.

True, though ... is used there as a type qualifier and is adjoined to the type.

Under this proposal, it would represent default return arguments and would be separated from the final non-default arguments by a comma. So, in this usage, I don't think it's unreasonable for it to represent a minimum of one argument and (to me at least) it would be both strange and potentially confusing for it do otherwise.

I'd be in favor of this proposal - in the restricted form described in @ianlancetaylor's latest post - though I'm not sure it's worth going beyond return ..., val in practice.

It's backwards compatible and looks like it should not be too difficult to implement. Whilst it can be argued that specifying individual return values is always clearer, an abbreviation seems reasonable when (in the case of a final error or bool) you don't care about the values of the other return values and so having to specify them is just noise.

One of the things I liked about the try proposal was that the default values of any other parameters would be returned, albeit under the hood, in the event of an error.

This proposal would achieve the same thing (and therefore save a little error handling boilerplate) but in a visible fashion which shouldn't upset those who didn't like try and would be optional in any case.

alanfo avatar Oct 29 '19 09:10 alanfo

Rather than return ...val operator, I would like to go with return _, val1,_, val2 approach. It gives more flexibility in inserting val anywhere without having to read a lot of rules around the operator. In functions having 2 or 3 return values, both are equally readable.

avi-yadav avatar Dec 17 '19 08:12 avi-yadav

@avi-yadav That would be the first time _ is used as an identifier to be read rather than written. This issue has come up before, and ... is actually more consistent with Go usage. For instance, in array declarations it stands for the count of the array, or as one might say here, "the obvious thing".

robpike avatar Dec 17 '19 09:12 robpike

My current thoughts on the three examples @robpike showed are

  1. allow all three
  2. discourage cases where ... represents 0 return values by linting
  3. have a tool that removes ... when it represents 0 return values

Allowing all three allows code generators working with a variable number of returns to avoid special cases. @carlmjohnson demonstrated a useful case earlier: paste the if err != nil { return ..., err } snippet regardless of the return-arity. Easy for the writer and it's easy to have any unnecessary ... removed on save or pre-commit for the reader.

return ... is the most interesting.

In the case where it could be rewritten return 0 there's little point. If the type of the value to return is unknown because it was created by code generation or it's a generic function and the syntactic form of the zero value is unknown, it's more useful.

In regular code, if ... stands for the zero of a struct value it's more concise, but that case would, imo, be better served by extending type elision.

There's also this case to consider:

func f() (T, error) {
  if someCond() {
    // zero of T suffices, no error
    return ...
  }
  // create a T in way that may fail
}

It would be strange to have to write return ..., nil there and return T{}, nil isn't much better. It's something of an uncommon case and deserves comment no matter what.

jimmyfrasche avatar Feb 29 '20 23:02 jimmyfrasche

Idle thought: What about named return values?

I can't figure out whether I'd expect return ..., err to overwrite them with zeros or leave them alone.

Intuitively, I think, I would expect a _ in a return list to imply not-overwriting a named return value which is already set, but I don't know why I'd expect that.

seebs avatar Mar 29 '20 18:03 seebs

This feature will be very fine when we have not goreturns with gopls

flibustenet avatar Mar 29 '20 19:03 flibustenet

I would just like to emphasize the parallel to array declarations, [...]. This is a fairly similar usage (getting rid of the obvious thing), and so I think it fits with go more than _, although I wouldn't mind that either.

lolbinarycat avatar Sep 03 '20 01:09 lolbinarycat

Toward to php?Why not named returns?

aleass avatar Jun 08 '22 10:06 aleass