You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+68Lines changed: 68 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -3677,6 +3677,74 @@ _You can enable the following settings in Xcode by running [this script](resourc
3677
3677
3678
3678
</details>
3679
3679
3680
+
*<a id='redundant-equatable-implementation'></a>(<a href='#redundant-equatable-implementation'>link</a>) **Prefer using a generated Equatable implementation when comparing all properties of a type.** For structs, prefer using the compiler-synthesized Equatable implementation when possible. [](https://github.com/nicklockwood/SwiftFormat/blob/main/Rules.md#redundantEquatable)
3681
+
3682
+
<details>
3683
+
3684
+
### Why?
3685
+
3686
+
Manually-implemented Equatable implementations are verbose, and keeping them up-to-date is error-prone. For example, when adding a new property, it's possible to forget to update the Equatable implementation to compare it.
3687
+
3688
+
```swift
3689
+
/// WRONG: The `static func ==` implementation is redundant and error-prone.
3690
+
struct Planet:Equatable {
3691
+
let mass: Double
3692
+
let orbit: OrbitalElements
3693
+
let rotation: Double
3694
+
3695
+
staticfunc==(lhs: Planet, rhs: Planet) ->Bool {
3696
+
lhs.mass== rhs.mass
3697
+
&& lhs.orbit== rhs.orbit
3698
+
&& lhs.rotation== rhs.rotation
3699
+
}
3700
+
}
3701
+
3702
+
/// RIGHT: The `static func ==` implementation is synthesized by the compiler.
3703
+
struct Planet:Equatable {
3704
+
let mass: Double
3705
+
let orbit: OrbitalElements
3706
+
let rotation: Double
3707
+
}
3708
+
3709
+
/// ALSO RIGHT: The `static func ==` implementation differs from the implementation that
3710
+
/// would be synthesized by the compiler and compared all properties, so is not redundant.
3711
+
struct CelestialBody:Equatable {
3712
+
let id: UUID
3713
+
let orbit: OrbitalElements
3714
+
3715
+
staticfunc==(lhs: Planet, rhs: Planet) ->Bool {
3716
+
lhs.id== rhs.id
3717
+
}
3718
+
}
3719
+
```
3720
+
3721
+
In projects that provide an `@Equatable` macro, prefer using that macro to generate the `staticfunc==` for classes rather than implementing it manually.
3722
+
3723
+
```swift
3724
+
/// WRONG: The `static func ==` implementation is verbose and error-prone.
3725
+
finalclass Planet:Equatable {
3726
+
let mass: Double
3727
+
let orbit: OrbitalElements
3728
+
let rotation: Double
3729
+
3730
+
staticfunc==(lhs: Planet, rhs: Planet) ->Bool {
3731
+
lhs.mass== rhs.mass
3732
+
&& lhs.orbit== rhs.orbit
3733
+
&& lhs.rotation== rhs.rotation
3734
+
}
3735
+
}
3736
+
3737
+
/// RIGHT: The `static func ==` implementation is generated by the `@Equatable` macro.
3738
+
@Equatable
3739
+
finalclassstruct Planet:Equatable {
3740
+
let mass: Double
3741
+
let orbit: OrbitalElements
3742
+
let rotation: Double
3743
+
}
3744
+
```
3745
+
3746
+
</details>
3747
+
3680
3748
*<a id='void-type'></a>(<a href='#void-type'>link</a>) **Avoid using `()` as a type**. Prefer `Void`.
0 commit comments