-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
interface-receiver: Clarify why value receiver can be called #170
Merged
+26
−6
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This may compile but it is not safe.
sPtrs[1]
will return a nil value if1
is not in the map, ands.Write
does not guard against a nil method receiver. (Nor should it: methods should be able to assume their receivers are valid, in general.)Expressions of the form
m[key].Method()
which chain map value lookups with method calls on those values are almost never safe. It would be great if this example could be removed from the guide.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed that this is an unsafe form and
m[key].Method()
should generally be avoided.However, I think this case was specifically demonstrating addressability of values -- the value stored in the map not being addressable, and therefore, not usable for the method.
I don't immediately have an alternative suggestion for how else to demonstrate addressable versus non-addressable right after the prior value-in-map example.
But again, I agree that we don't want to encourage
m[key].Method()
, so, short of a different way to make the point this is making, maybe a comment would suffice? Something like "This sample is just to demonstrate the non-addressable nature of values stored in maps. We don't recommend calling methods on map values directly. See [link]" -- possibly accompanied by a new style guide entry that discouragesm[key].Method()
-- although we'll probably want to discuss it with the maintainers in a new issue. I think there's some nuance to it besides "don't call methods on map items directly," e.g. "unless you're absolutely certain" or "unless you control all keys". Perhaps "be wary" is better than "don't", but I'll defer that to the discussion.Edit: Opened #178