diff --git a/README.md b/README.md index a3dcfa0..6df1cb0 100644 --- a/README.md +++ b/README.md @@ -3275,6 +3275,50 @@ _You can enable the following settings in Xcode by running [this script](resourc +* (link) **Prefer defining simple generic constraints in the generic parameter list rather than in the where clause.** [![SwiftFormat: simplifyGenericConstraints](https://img.shields.io/badge/SwiftFormat-simplifyGenericConstraints-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/main/Rules.md#simplifyGenericConstraints) + +
+ + #### Why? + Inline generic constraints (``) are more concise and idiomatic than where clauses (` where T: Protocol`) for simple protocol conformances. Using inline constraints for simple cases makes generic declarations easier to read at a glance. Where clauses are reserved for complex constraints that cannot be expressed inline, like associated type constraints (`T.Element == Star`) or concrete type equality. + + ```swift + // WRONG + struct SpaceshipDashboard: View + where Left: View, Right: View + { + ... + } + + extension Spaceship { + func fly( + to: Destination, + didArrive: (Destination) -> Void + ) where Destination: PlanetaryBody { + ... + } + } + + // RIGHT + struct SpaceshipDashboard: View { + ... + } + + extension Spaceship { + func fly( + to: Destination, + didArrive: (Destination) -> Void + ) { + ... + } + } + + // ALSO RIGHT: Complex constraints remain in where clause + struct Galaxy where T.Element == Star {} + ``` + +
+ * (link) **Default type methods to `static`.**
diff --git a/Sources/AirbnbSwiftFormatTool/airbnb.swiftformat b/Sources/AirbnbSwiftFormatTool/airbnb.swiftformat index eb683e4..3e3ebe3 100644 --- a/Sources/AirbnbSwiftFormatTool/airbnb.swiftformat +++ b/Sources/AirbnbSwiftFormatTool/airbnb.swiftformat @@ -143,3 +143,4 @@ --rules redundantBreak --rules redundantTypedThrows --rules preferFinalClasses +--rules simplifyGenericConstraints \ No newline at end of file