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-7293] Swift local refactoring: Customize Equatable Conformance #49841

Closed
nkcsgexi opened this issue Mar 27, 2018 · 15 comments
Closed

[SR-7293] Swift local refactoring: Customize Equatable Conformance #49841

nkcsgexi opened this issue Mar 27, 2018 · 15 comments
Labels
conformances Feature → protocol: protocol conformances Equatable Area → standard library: The `Equatable` protocol 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

nkcsgexi commented Mar 27, 2018

Previous ID SR-7293
Radar None
Original Reporter @nkcsgexi
Type New Feature
Status Closed
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Source Tooling
Labels New Feature, Refactoring, StarterBug
Assignee andrewtkachuk (JIRA)
Priority Medium

md5: afc6ec2379362a2c31c09e7d29216645

relates to:

  • SR-7291 Swift local refactoring: adding Hashable conformances automatically

Issue Description:

A typical way of implementing equatable conformance is to check equal for all fields. A local refactoring can be implemented to help this pattern, e.g.

// Right click on the declared name Animal and apply "adding Equatable 
// conformance" refactoring. 
class Animal {
 var age: Int
 var kind: AnimalKind
 var gender: Gender
 init(age: Int, kind: AnimalKind, gender: Gender) {
   self.age = age
   self.kind = kind
   self.gender = gender
 }
}

When a user right-clicks the declared name "Animal", an available refactoring "adding Equatable conformance" appears. After applying the refactoring, the above code snippet transforms to:

// Right click on the declared name Animal and apply "adding Equatable 
// conformance" refactoring. 
class Animal: Equatable {
  var age: Int
  var kind: AnimalKind
  var gender: Gender
  init(age: Int, kind: AnimalKind, gender: Gender) {
    self.age = age
    self.kind = kind
    self.gender = gender
  }
  // Auto-generated function
  static func == (lhs: Animal, rhs: Animal) -> Bool {
    return lhs.age == rhs.age &&
           lhs.kind == rhs.kind &&
           lhs.gender == rhs.gender
  }
}

This code after transformation may not be the end outcome the user expected. However, they can always adjust if needed.

@belkadan
Copy link
Contributor

This is no longer needed for structs and enums. Do you think it's worth having for classes, though? cc @allevato

@belkadan
Copy link
Contributor

I suppose one possible use is to make it explicit so you can modify it.

@allevato
Copy link
Collaborator

I agree—given that there's no annotation or other mechanism to exclude individual properties from ==, a local refactor would still be a valuable time-saver for users who want to generate it and then strip out a couple of the properties.

@swift-ci
Copy link
Collaborator

Comment by Mohit Athwani (JIRA)

If this is still needed and if no one is working on it, I'm willing to take this up 🙂

@swift-ci
Copy link
Collaborator

swift-ci commented Aug 3, 2018

Comment by Mohit Athwani (JIRA)

@nkcsgexi

@nkcsgexi
Copy link
Member Author

nkcsgexi commented Aug 3, 2018

Sure, Mohit. I've just assigned it to you.

@swift-ci
Copy link
Collaborator

swift-ci commented Aug 3, 2018

Comment by Mohit Athwani (JIRA)

@nkcsgexiThanks! I'm going to finish writing a blog post on how I attempted my previous PR and then got moving with this 🙂

@nkcsgexi
Copy link
Member Author

nkcsgexi commented Aug 3, 2018

Sounds great! We should rename this refactoring to "Customize Equatable Conformance" because using synthesized conformances is highly recommended.

@swift-ci
Copy link
Collaborator

Comment by Mohit Athwani (JIRA)

@nkcsgexi Finally got the time to start working on this. Updated all the repositories and tried to build with --debug --Xcode on Mojave and Xcode 10 and I got a Build Failed error. has something changed under the new os version?

@nkcsgexi
Copy link
Member Author

hmm, the build instruction should stay the same. What failure did you see?

@swift-ci
Copy link
Collaborator

swift-ci commented Oct 6, 2018

Comment by Mohit Athwani (JIRA)

There's multiple clang errors. Got it fixed from this link: https://bugs.swift.org/browse/SR-7961

@belkadan
Copy link
Contributor

Resetting assignee on all Starter Bugs not modified since 2018.

@swift-ci
Copy link
Collaborator

Comment by Andrii Tkachuk (JIRA)

Hi! I would like to work on this improvement.

cc @nkcsgexi

@swift-ci
Copy link
Collaborator

Comment by Andrii Tkachuk (JIRA)

Here is the PR: #29847

@swift-ci
Copy link
Collaborator

swift-ci commented May 7, 2021

Comment by Andrii Tkachuk (JIRA)

Included in Swift 5.3.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added source tooling Area: IDE support, SourceKit, and other source tooling feature A feature request or implementation conformances Feature → protocol: protocol conformances Equatable Area → standard library: The `Equatable` protocol and removed new feature labels Jan 28, 2023
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
conformances Feature → protocol: protocol conformances Equatable Area → standard library: The `Equatable` protocol 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