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: add 'implements' compiler hint for interface implementations #28741
Comments
I think the convention here is a typed package-level variable assigned to the blank identifier: var _ Foo = (*Baz)(nil)
var _ Bar = (*Baz)(nil) If |
Thank, I did not know about this convention yet, seems like it will help. Seems like a workaround for a missing language feature though! |
What do you mean? It should say which function it caught that isn't implemented.
I have a few issues with this syntax:
Also, we have a way to do this already:
|
Despite working quite a lot with the language for about 6 weeks or so (like 200 hours!) I did not know about this convention Perhaps then the proposal is to elevate this somewhat cryptic convention to a language feature.
Honestly the difference between pointer types and non-pointer types as it relates to interface implementations is one of the most confusing things I've encountered in the language thus far, so I can't really comment on that until I figure out how it works. There's ambiguity in my mind about what it means to be a pointer vs a non-pointer when I am an interface. And I haven't fully understood, when implementing an interface, what it means to have pointer vs. non-pointer receivers. I basically do one or the other and then go try combinations of & and * until it works. I'm mostly a C++ programmer, if that explains it. In fact in the example:
It is not currently clear to me why both of these would be necessary. It seems like one or the other would suffice. Is there some reading that would help me here? In regards to 'encouraging defining structs around interfaces' that argument is a bit dogmatic for my taste. I guess there's some fear of Golang being used in way that looks too much like traditional OO? |
I think that adding a new keyword to the language is a pretty heavyweight approach for an optional mechanism that can already be done in a different way. |
I was unaware of the convention to assign a struct to an unnamed instance of the interface. |
In terms of this, I meant more to show that you would do |
Yes, I see that only one or the other is possible depending on which type of receiver the implementation has. I still don't understand pointer/non-pointer receivers and the semantics of the interface usage, but thankfully the compiler does! 👍 |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
I started programming with Go about a month ago.
It is truly an awesome language. However, I think a lot of time could be saved by adding an optional hint to the compiler when I am implementing an interface. When I, optionally, declare to the compiler that my struct implements a particular interface, the compiler should fail to compile when that struct has any missing/incorrect methods/signatures.
Example:
In this case, Baz would fail to compile if any of the signatures of Foo or Bar are missing or incorrect, and the compiler could provide very specific information about the incorrect or missing signatures, at the site of Baz (instead of inferring my intent from a misuse of Baz).
This is a breaking change, but only if I choose to use it. If I want my code to compile with earlier versions of the language, then I can use this feature temporarily to help fix issues, then remove them for my final check-in. Tooling to remove these hints would be quite simple (it could be done easily with a Regex search and replace in Sublime, for example).
In short, having the option to declare my intention to implement an interface seems like it could only improve, not detract from, the language.
What did you expect to see?
Better information from the compiler and IDE about struct compatibility with interfaces.
What did you see instead?
Failures to compile because my struct is not compatible with the desired interface, at times without helpful information about why the struct is incompatible with the interface.
The text was updated successfully, but these errors were encountered: