Skip to content

Commit f0bbb0b

Browse files
committed
Add rule to infer property types from the right-hand-side value rather than writing the type explicitly on the left-hand side
1 parent 0f14982 commit f0bbb0b

File tree

2 files changed

+61
-13
lines changed

2 files changed

+61
-13
lines changed

README.md

+59-12
Original file line numberDiff line numberDiff line change
@@ -333,25 +333,72 @@ _You can enable the following settings in Xcode by running [this script](resourc
333333

334334
```swift
335335
// WRONG
336-
let host: Host = Host()
336+
let sun: Star = Star(mass: 1.989e30)
337+
let earth: Planet = Planet.earth
337338

338339
// RIGHT
339-
let host = Host()
340+
let sun = Star(mass: 1.989e30)
341+
let earth = Planet.earth
342+
343+
// WRONG: Most literals provide a default type that can be inferred.
344+
let enableGravity: Bool = true
345+
let numberOfPlanets: Int = 8
346+
let sunMass: Double = 1.989e30
347+
348+
// RIGHT
349+
let enableGravity = true
350+
let numberOfPlanets = 8
351+
let sunMass = 1.989e30
352+
353+
// WRONG: Types can be inferred from if/switch expressions as well if each branch has the same explicit type.
354+
let smallestPlanet: Planet =
355+
if treatPlutoAsPlanet {
356+
Planet.pluto
357+
} else {
358+
Planet.mercury
359+
}
360+
361+
// RIGHT
362+
let smallestPlanet =
363+
if treatPlutoAsPlanet {
364+
Planet.pluto
365+
} else {
366+
Planet.mercury
367+
}
340368
```
341369

370+
</details>
371+
372+
* <a id='infer-property-types'></a>(<a href='#infer-property-types'>link</a>) **Prefer letting the type of a property be inferred from the right-hand-side value rather than writing the type explicitly on the left-hand side.** [![SwiftFormat: preferInferredTypes](https://img.shields.io/badge/SwiftFormat-preferInferredTypes-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#preferInferredTypes)
373+
374+
<details>
375+
342376
```swift
343-
enum Direction {
344-
case left
345-
case right
346-
}
377+
// WRONG
378+
let sun: Star = .init(mass: 1.989e30)
379+
let earth: Planet = .earth
347380

348-
func someDirection() -> Direction {
349-
// WRONG
350-
return Direction.left
381+
// RIGHT
382+
let sun = Star(mass: 1.989e30)
383+
let earth = Planet.earth
351384

352-
// RIGHT
353-
return .left
354-
}
385+
// ALSO RIGHT: Explicit types are required when there is no right-hand-side value.
386+
let sun: Star
387+
let earth: Planet
388+
389+
// ALSO RIGHT: Explicit types can be necessary when the right-hand side has
390+
// a different type from the one written explicitly on the left-hand side.
391+
let nautralSatellite: PlanetaryBody? = Moon(mass: 7.347e22)
392+
let moon: PlanetaryBody? = nil // nil literals are typeless
393+
let numberOfPlanets: UInt = 8 // integer literals default to `Int`
394+
let sunMass: CGFloat = 1.989e30 // floating-point literals default to `Double`
395+
let planets: [Planet] = [] // empty collection literals are typeless
396+
397+
// ALSO RIGHT: Some of the examples above can also be written idiomatically without an explicit type on
398+
// the left-hand-side, by instead giving the right-hand side value an explicit type. Either style is fine.
399+
let numberOfPlanets = UInt(8)
400+
let sunMass = CGFloat(1.989e30)
401+
let planets = [Planet]()
355402
```
356403

357404
</details>

Sources/AirbnbSwiftFormatTool/airbnb.swiftformat

+2-1
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,5 @@
100100
--rules wrapMultilineConditionalAssignment
101101
--rules blankLineAfterMultilineSwitchCase
102102
--rules consistentSwitchStatementSpacing
103-
--rules semicolons
103+
--rules semicolons
104+
--rules preferInferredTypes

0 commit comments

Comments
 (0)