-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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: remove redundant struct specifier on methods declarations #29459
Comments
Code I contribute to often has long type names (on the order of the example) and writing them over again is not a big issue (vim-go or other editor plugins help). I think the added syntactic sugar shown above doesn't provide much value for the cost of having two ways to write the same thing. |
This would also add ambiguity to the language syntax. When the parser sees
Go has very few ambiguities like those at the moment, so I think we should be very careful when adding more. Another point against this change that I see is that we already have the |
For the record: since any type can have methods, this should clearly apply to any type, not just struct types. (But I think it is unlikely that we would adopt this change.) |
Actually no.
As I know, golang tries to be tool-independent. On early days of the language the core team declared that even code highlighting would be not needed for the golang due it's tiny and well composed syntax. My proposal follows the idea that language should be simple for writing and reading without any kind of additional tools. Another point here is the next: in case if this kind of syntax was introduced by the language reasonable earlier, what kind of 2 types to write methods of structs would you choose today: shorter or longer? |
In this case
Agree that "Go should be easy to read and understand". |
The parser should know how to parse a top-level declaration just by looking at each token. Remembering earlier type declarations, and peeking at tokens to see if there's an open parentheses, is precisely what would make the Go syntax much more complex. Just from looking at
Your new proposed syntax is very similar to an existing syntax that does something very different - I'm only pointing that out.
We agree to disagree. Languages that try to be fast to write by saving characters at all costs tend to be the less readable ones in the long run. |
This would certainly make readability worse, in exchange for the dubious benefit of saving a few keypresses when typing code. A non-starter, in my view. |
Currently all Go top level declarations are stand alone. This would break that feature. It would mean that introducing a new type definition between existing methods could completely break the program. The resulting code seems less readable. This means that either you can't move a method declaration to a different file, or there are two different syntaxes for method declaration which seems like unnecessary duplication. If the concern is long type names for readability, you can always use an alias for the method declarations. Thanks, but we aren't going to make this change. |
The standard way to declare the method for some struct is the next:
If there are only few methods in the struct - it is ok, but if you have relatively big collection of methods - duplication of the struct specifier goes boring very quickly. For the structures which names are long - the problem goes further and significantly increases the code base size and decreases readability, for example:
The proposal is to simplify the syntax to the next way:
The presence of an argument between the keyword
func
and the method's name automatically indicates that this method belongs to the struct. We do not need any parentheses or redundant struct names specified.With this improvement the code for long named structs would look like this:
The code for the cases when there are several structs defined in common file:
One more important thing here is the potential performance boost.
Compiler would be able to identify corresponding struct significantly faster with this syntax, because it would be able to simply store the last seen struct and attach all methods seen further to it automatically. No more any receiver's name parsing needed.
The text was updated successfully, but these errors were encountered: