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
+59-12
Original file line number
Diff line number
Diff line change
@@ -333,25 +333,72 @@ _You can enable the following settings in Xcode by running [this script](resourc
333
333
334
334
```swift
335
335
// WRONG
336
-
let host: Host =Host()
336
+
let sun: Star =Star(mass: 1.989e30)
337
+
let earth: Planet = Planet.earth
337
338
338
339
// 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
+
}
340
368
```
341
369
370
+
</details>
371
+
372
+
* <aid='infer-property-types'></a>(<ahref='#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.**[](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#preferInferredTypes)
373
+
374
+
<details>
375
+
342
376
```swift
343
-
enumDirection {
344
-
caseleft
345
-
caseright
346
-
}
377
+
// WRONG
378
+
let sun: Star = .init(mass: 1.989e30)
379
+
let earth: Planet = .earth
347
380
348
-
funcsomeDirection() -> Direction {
349
-
// WRONG
350
-
return Direction.left
381
+
// RIGHT
382
+
let sun =Star(mass: 1.989e30)
383
+
let earth = Planet.earth
351
384
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.
0 commit comments