Skip to content
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

Closed
nkcsgexi opened this issue Mar 27, 2018 · 9 comments
Closed
Assignees
Labels
compiler The Swift compiler in itself feature A feature request or implementation good first issue Good for newcomers refactoring Area → source tooling: refactoring source tooling Area: IDE support, SourceKit, and other source tooling

Comments

@nkcsgexi
Copy link
Member

Previous ID SR-7291
Radar None
Original Reporter @nkcsgexi
Type New Feature
Status Closed
Resolution Won't Do
Additional Detail from JIRA
Votes 0
Component/s Source Tooling
Labels New Feature, Refactoring, StarterBug
Assignee @nkcsgexi
Priority Medium

md5: b321df4bfee75aa02d940890c1269ac4

relates to:

  • SR-7293 Swift local refactoring: Customize Equatable Conformance

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.

// Right click on the name "Animal"
class Animal {
  var age = 0
}

An available refactoring "add hashable conformance" shows up when users right-click on "Animal". After applying the refactoring, we change the code example to:

// Right click on the name "Animal"
class Animal: Hashable {
  var age = 0
  // Auto-generated code
  var hashValue: Int = <#value#>
  static func == (lhs: Animal, rhs: Animal) -> Bool {
    return lhs.hashValue == rhs.hashValue
  }
  // Auto-generated code
}

Notice this refactoring may require little semantic information so that we can implement it by using libSyntax APIs in Swift.

@belkadan
Copy link
Contributor

Ah, we definitely don't want to generate an == that uses hash values! That's not correct.

Same questions as SR-7293: there was a reason we didn't add this for classes in the compiler; do we really want to add it as a refactoring? cc @allevato

@belkadan
Copy link
Contributor

Also, the hashing API is about to change, so I suggest people not take this yet until that's settled down. cc @lorentey

@lorentey
Copy link
Member

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.

var hashValue: Int {
  return <#combined value#>
}

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!

@nkcsgexi
Copy link
Member Author

Ok, I think i'm about to close this since relying on compiler to synthesize these conformances is a recommended strategy.

@nkcsgexi
Copy link
Member Author

since we'll start to synthesizing the Hashable conformance, the refactoring may send the wrong message

@allevato
Copy link
Collaborator

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.

@nkcsgexi
Copy link
Member Author

@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.

@lorentey
Copy link
Member

I'm preparing a proposal to fix Hashable; if it passes, it will become possible to implement this refactoring well.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added compiler The Swift compiler in itself feature A feature request or implementation source tooling Area: IDE support, SourceKit, and other source tooling and removed new feature labels Nov 23, 2022
@AnthonyLatsis
Copy link
Collaborator

@nkcsgexi Do you remember what the resolution here was? Was the refactoring implemented or revoked in favor of derived conformances?

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler in itself feature A feature request or implementation good first issue Good for newcomers refactoring Area → source tooling: refactoring source tooling Area: IDE support, SourceKit, and other source tooling
Projects
None yet
Development

No branches or pull requests

5 participants