-
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: An Improved, Golang-Cohesive Design for Named Arguments #12296
Comments
I will be cc-ing my responses to comments on Google Docs here for reference |
Sorry. Yes, for implementation simplicity... all arguments must be named and present. |
Additional concern: so now the argument names become part of the API signature, and thus part of the Go 1 API contact? Currently they're just a documentation and implementation detail. But with this proposal, we could never rename them. |
I'll be honest; It's possible. But are there not many language features that could be abused? The fact that functions are first class objects and are passed around could be abused. Someone could design everything using asynchronous callbacks encouraged by NodeJS. It's callback hell. It's not encouraged, but it's possible. Why would anyone ever use all of these callbacks over the asynchronous functions in Go? Why would anyone put too many parameters illogically? In my book, they both could have potential disadvantages. As for renaming, you could offer aliases like Swift does: |
I think the idiomatic Go solution is not creating functions with a lot of
arguments, and use a struct when necessary. This also has further benefits
(zero values don't need to be specified.)
|
A compromise that would be in line with other recent changes to the language would be to infer from the func signature the type of struct literals passed in, as for elements of slices and maps, e.g.:
This still provides a relatively lightweight named-arg-like syntax for calling the function, though it does require declaring the struct type. |
If we really want to do something here (I suppose we're not),
It provides both named parameter and default (zero) parameters. |
Allowing elision of the type in a composite literal (struct or otherwise) in a function call covers everything without adding new concepts to the language. The only syntactic difference is there'd have to be outer {} but that seems a small price and would allow things like: It has the same problem of any such proposal though where it makes it hard to tell what the type of the literal is at a glance. But you could say that for every other argument to a function, including composite literals without the type elided as they could be used either for that type or are being assigned to an interface. That is, is Cheney's "functional options" pattern provides a nice alternative to structs as named parameters that can be useful in either case: http://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis |
There's much debate and feedback already on the linked Google Docs. @codeblooded perhaps you may want to consolidate the feedback in one place or suggest where (here or in the Google doc) you want additional input. There is a deep connection between struct (also called records in other languages) and function parameters (= which are part of the activation record ). It is very intriguing to have similar notation for setting up structs and invoking functions. I do like that the proposal is pretty minimal, but that said, here are some of the concerns brought up so far, plus a few new ones:
My conclusion so far, certainly as far as language design is concerned: When in doubt, leave it out! I think permitting automatic deduction of a struct composite literal from assignment context (not just when used inside another composite literal) has a much higher chance of being adopted (@minux, point 2, I also think there's already a proposal or discussion about this elsewhere). This would permit struct parameters and simple passing of struct values w/o the need of the struct type. E.g.:
|
I second @minux's comment (#12296 (comment)), but would like to point out that only struct type elision (point 2) is necessary, since anonymous struct types can already be declared in a function parameter list and the function can simply give that argument a name and access its fields like usual. I think @griesemer may have been thinking this, but to carry it through, the following is already possible:
I think this has several advantages over this proposal. It doesn't expose any API that isn't already exposed, so people don't find themselves locked in to their argument name choices. It upholds that rule that public identifiers must be capitalized. It has the dual properties that zero values can be omitted and the API can be extended with more fields in the future, which is a very common application for named parameters. And, the concept and syntax mirror the type elision that we already permit in map, array, and slice literals. |
@aclements Exactly my thoughts. See also my example code (3rd line, use of a struct literal in parameter list). |
You're right. I agree. This would break function assignment with identical arguments and different names, unless we used aliases which I think is overkill. After much thought, I have found Inferring struct types at function call (addressed by @abglassman @minux @jimmyfrasche @griesemer @aclements) to be the most elegant compromise and a solution to this problem. I think it organizes it well, and I think it does not result in any clashes with existing Go-ideology. Are there any objections or feedback on this idea? Should I proceed to create a |
Yeah, I think it should be a separate proposal. |
@codeblooded Yes, please, start a separate proposal. Thanks. |
Named parameters have been requested in the past, and they have been rejected with good reason. However, I have approached this language feature from a different angle. I feel like it would improve legibility and maintainability throughout Go libraries and executables.
I have written up a proposal on Google Docs here: goo.gl/8KdnEG. Some contributors have already begun commenting.
I just learned of the new proposal process. I would love some feedback. Should I go ahead and fork
golang/proposals
and modify the doc to meet the new template or wait?The text was updated successfully, but these errors were encountered: