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
[SR-7291] Swift local refactoring: adding Hashable conformances automatically #49839
Comments
Also, the hashing API is about to change, so I suggest people not take this yet until that's settled down. cc @lorentey |
Indeed; I'm working on a proposal to overhaul Hashable. If it goes through, hashValue will be replaced with a function requirement. An additional point on the template above: declaring hashValue as a stored property is almost never what we want – in the vast majority of cases, it should be declared as a calculated property.
The problem is, of course, that it's entirely non-obvious what expression people should write in the "combined value" slot. Hence the proposal I'm drafting! |
Ok, I think i'm about to close this since relying on compiler to synthesize these conformances is a recommended strategy. |
since we'll start to synthesizing the Hashable conformance, the refactoring may send the wrong message |
Since `Hashable` is hard to get right, any code generated by a local refactoring either (1) would need to be nearly as good as what the compiler generates for value types, as a starting point for users to edit, or (2) would be extremely generalized to the point of being useless. The catch is that the autogenerated `hashValue` for value types uses stdlib-private functions to provide a quality implementation, and a local refactor would not be able to do that. So item 1 above wouldn't be easy to achieve. It's probably best left out. |
@allevato I think you're right. The best strategy for the developers is to rely on compiler to synthesize these conformances. We should encourage them to avoid manually typing these boilerplate as much as we can. I've just closed this task. |
I'm preparing a proposal to fix Hashable; if it passes, it will become possible to implement this refactoring well. |
@nkcsgexi Do you remember what the resolution here was? Was the refactoring implemented or revoked in favor of derived conformances? |
Additional Detail from JIRA
md5: b321df4bfee75aa02d940890c1269ac4
relates to:
Issue Description:
Developers use Hashable conformances a lot and many hashable implementations fall into patterns. A local refactoring can be implemented to help developers save some manual typing effort, e.g.
An available refactoring "add hashable conformance" shows up when users right-click on "Animal". After applying the refactoring, we change the code example to:
Notice this refactoring may require little semantic information so that we can implement it by using libSyntax APIs in Swift.
The text was updated successfully, but these errors were encountered: