-
Notifications
You must be signed in to change notification settings - Fork 17.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: Go 2: specify default value in named return signature #31266
Comments
What is the difference between func MyFunc(x int) (result int = 42) {
...
} and func MyFunc(x int) (result int) {
result = 42
...
} They seem essentially the same to me. |
Wouldn't it be clearer to the users what the expected default will be? |
Default parameters do matter, but have default results any meaningfulness? |
Even though I like default input params, I feel like default output parameters are definitely less useful.
You'd still need to read the entire method and verify that the result was not changed within the body.
So the only possible use I can see is for removing one line in guard statements (like @ianlancetaylor wrote). But I feel like there is a general risk of making code more obscure. Default inputs are useful because you can guarantee the method has at least variables with certain parameters, and can help with avoiding some guards by providing sane defaults. I feel like the same can not be said for this proposal :) |
Okay. I learned a lot from the feedback. Thank you very much! I'll close this proposal then. Is there already a proposal for default inputs as you mentioned @DylanMeeus? |
It is rejected: #21909 |
I see.. Thanks! |
Background
Currently go has the following syntax for named returns:
Proposal
I would like to propose that we add syntax for specifying the default value in the named return:
Essentially, this proposal is for syntactic sugar in place of a
var defaultValue ReturnType = value
, which I believe will allow for some code cleanliness.Use Case
The proposed signature would make the function equivalent to the current implementation, but replacing the zero-value at compile-time.
There are other useful cases as well, such as returning the input arguments by default.
Interfacing
The named returned should not affect its function signature, ie. the following functions are all signature-equivalent
Also, the equality for interface values should follow the standard go syntax
Comments
I believe that having a default value specified at compile-time is a useful construct, and saves 1 operation of declaring a default result value, like so:
Of course this can already be done by duplicating them inline during the
return
statement, such as this example:but it seems that it would look a lot cleaner like this:
The text was updated successfully, but these errors were encountered: