-
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: hash: make the Sum method less confusing #21070
Comments
I don't see that happening anytime soon. I think |
I agree that renaming the |
In go 1, we could rename the argument
This is just a cosmetic, backwards compatible change but it can help avoid the confusion. |
This may not be worth the pain. We'd need to make a new v2 hash package, and then either make v2s of all implementations, or add an additional |
Why does the method need to be called AppendSum? What other Append operations could it possibly be referring to? If have an Appender interface type with the signature The original name |
@bradfitz Because of the "pain" you mention, I believe if this might ever be done, "Go2" is the only point where it could be. Please don't casually dismiss it too easily. Please note there's also a huge pain in the current state of events. Because of this very issue, I personally have a huge yellow virtual "WARNING" sign attached to the whole hash package in my mind, with the small print saying: "never touch the hash package without cautiously reading the godoc; there's some well-known minefield somewhere there". And I'm certainly not a noob to Go, being in the community since the 2009. Notably, I do also believe that the current shape of the interface can be unnecessarily risky with regards to use in security-related applications (there's a reason why many Hash implementations are located in the crypto package), and in this domain I believe it's generally better to be safe than sorry, including in API design. For the sake of making the virtual world somewhat safer. I understand this may not be a "prime time" candidate for the "Go2" process. But once some of the necessary Go2 refactoring approaches/mechanisms get tested and proven on some more "attractive" packages/issues, and people get used to them in the Go 2 transition period, I don't see why the "smaller papercuts" issues like this one cannot get pulled onto the bandwagon as well. |
Any plan? it's highly useful to minimize memory overhead when the go program will do the sum BTW: Append API naming style is idiomatic:) |
This proposal has been added to the active column of the proposals project |
There are many things we can redo in v2's of packages, but changing the names of methods in widely-used interfaces does not seem to be one of them. For the same reason I don't think we can change, for example, that zero-length Reads are allowed. We can't add a method to hash.Hash, and some packages only return a hash.Hash, so we can't add methods to an exported concrete implementation because there isn't one. It seems like the best we can do is provide better examples in the affected packages, and perhaps also a non-executable example in the doc comment for the Sum methods. |
Perhaps this would be a sufficient addition to the doc comment: // Sum appends the current hash to b and returns the resulting slice.
// It does not change the underlying hash state.
//
// To store the hash into an array of the appropriate size,
// use the following form (illustrated for sha256):
//
// var hash [sha256.Size]byte
// h.Sum(hash[:0])
Sum(b []byte) []byte |
Based on the discussion above, this proposal seems like a likely decline. |
No change in consensus, so declined. |
The Go 1
hash.Hash
interface has aSum
method:Individual packages (such as
md5
) containSum
functions with very similar signatures but totally different meaning:This makes
(hash.Hash).Sum
confusing, as illustrated in the review for http://golang.org/cl/49030.I can see two possible improvements.
AppendSum
(along the lines of thestrconv
Append
functions). Adding such a method to the implementations (but not theHash
interface itself) would be backward-compatible with Go 1.Sum
to not accept a parameter and always return a new slice. If we pair that with appropriate inlining and devirtualization optimizations, it could theoretically be as efficient as appending to an existing slice.I recommend option (1), because I think it would migrate more smoothly in binaries that mix Go 1 and Go 2.
The text was updated successfully, but these errors were encountered: