-
Notifications
You must be signed in to change notification settings - Fork 17.6k
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: initialise funcs with custom type names #36855
Comments
See also this issue: #21498 |
Thanks for filling out the template. I'm troubled by the fact that this permits expressions to refer to identifiers that are only defined far away in a different package. It means that changing the name of an unexported identifier in one package can break a different package that the author may not even know about. One of the goals of Go is to support programming at scale, and this suggestion seems to make that significantly harder. |
Can you clarify what unexported identifiers you are referring to? Though wagering a guess, I assume you mean the function parameters? If this is correct, then the implication, which I did not explicitly call out is: parameter names, once defined, are part of the external interface, ant changing them is a breaking change. Since there is no usage for the names today, this is not a breaking change. I agree there is some cognitive load, but it is unambiguous what parameter names are, and this information is easily accessible via go doc. Furthermore, much like unkeyed struct literals or type inference, this change trades unambiguous ceremony for concise expressiveness. This new syntax also does not rule out the existing one. There may be places, where authors prefer the existing syntax, but there are clear places where the new one is much more readable. |
I don't see how this works in terms of the language grammar. How does the parser know to parse the |
This speaks to my limited knowledge of the compiler/parser implementation details. I cannot disagree that the ambiguity you describe may exist, but each block does have an unambiguous format. This seems similar to the current ambiguity between how |
Yes, I meant the function parameters. Sorry for not being clear. I agree that this proposal means that changing the name of a function parameter changes the external API of an exported function. My point is that this is unlike anything in Go today. In other words, this is not a small syntactic addition; it's a subtle and far reaching change to the definition of the API of a Go package. |
With regard to @neild 's point, the syntax of a composite literal is the same regardless of the type of the composite literal. The type of the composite literal determines the kind of values that can appears in the composite literal, but that does not affect the syntax any more than the fact that It's a critical feature of Go that it be possible to parse the language without using a symbol table, so I do think that this is a blocker for the suggested syntax. |
As noted above, this is a subtle change to how the Go API works across packages, and the syntax is most likely ambiguous. Also, the proposal does not have strong support based on emoji voting. For these reasons this is a likely decline. Leaving open for four weeks for further comments. |
I can understand these concerns, and agree the additional exposed symbols represent a coup de grâce. Upon review of the added features (interface implementation, simple initialisation, and method set access), the main benefit is interface implementation. This can be equivalently well served by #21670, however the discussion has stalled. Can I push for renewed discussion on that proposal? Another option is #25860, since |
No change in consensus, so closing. @carnott-snap Sure, feel free to try to push forward on any other issue. Thanks. |
Intermediate to experienced.
This change adds a new way to construct functions, so implicitly it will increase the total knowledge require to understand all of Go. It is a feature that can be obscured from beginners, and benefit intermediate to experienced users.
There are no github issues for this exact proposal.
#21670 and #25860 only seeks to solve the implement an interface example below. #21670 only targets single method interfaces, while not adding any language changes, where #25860 seeks to solve this for all interfaces, with a new (somewhat complex) syntax.
This helps developers that create anonymous functions that implement defined types.
Yes, the func types currently cannot take a body.
This change will add new syntax, thus complexity. This is most clear in the use of the default
func
parameter names,function{return from+to}
, however, this feels very similar to the existing unkeyed struct literal syntax:type structure struct { u uint }; _ = structure{5}
. This will also require some changes to existing symbols, liketype HandlerFunc func(r ResponseWriter, r *Request)
.As the syntax of the language is extended, in a compatible manner, all these tools would need minor updates.
While this will add complexity to the compiler, it is pure syntax sugar, so it should not be too expensive.
Should be negligible, as this is syntax sugar.
I am not familiar enough with the internals of the compiler to make a concrete suggestion, but high level:
Type
that is of typefunc
followed by a body:Type{ /* ...*/ }
Type(func(/* ... */) (/* ... */) { /* ... */ })
No
The declarations section, not totally sure where, would need to call-out that
Type { Expression ( ; Expression ) }
is valid.This change will extend the way that functions can be initialised. It should not break any existing features, but can simplify some existing code.
No.
No.
No.
The text was updated successfully, but these errors were encountered: