nicklockwood / SwiftFormat

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

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

Open jshier opened 2 hours ago

jshier commented 2 hours 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 2 hours 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 hour ago

A similar example related to literals is:

let width: Double = 10
// vs
let width = Double(10)
nicklockwood commented 1 hour 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 hour ago

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