nicklockwood / SwiftFormat

A command-line tool and Xcode Extension for formatting Swift code
MIT License
7.95k stars 640 forks source link

[New Rule]: A standard style for empty collection inits #1887

Open jshier opened 1 month ago

jshier commented 1 month ago

Barring other considerations, these two forms mean the same thing for Array and Set:

let x: [Int] = []
let x = [Int]()

let y: Set<Int> = []
let y = Set<Int>()

I don't see a rule to allow preferring the first form over the second, is it possible?

calda commented 1 month ago

It could make sense to support this as an option in the propertyType rule. Agreed this would be a good addition.

calda commented 1 month ago

A similar example related to literals is:

let width: Double = 10
// vs
let width = Double(10)
nicklockwood commented 1 month ago

@calda it was at one point recommended not to use Double(0) because it's not merely an alternative syntax for casting; it actually incurs an additional runtime cost:

https://stackoverflow.com/questions/42705484/why-literals-produce-more-efficient-code-than-initializers

I've no idea if that's still the case now though. In any case the cost is presumably trivial.

jshier commented 1 month ago

If you did want to that, it should probably be a separate setting or a configuration option separate from the collection values.