Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jshier opened this issue Sep 25, 2024 · 4 comments
Open

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

jshier opened this issue Sep 25, 2024 · 4 comments

Comments

@jshier
Copy link

jshier commented Sep 25, 2024

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
Copy link
Collaborator

calda commented Sep 25, 2024

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

@calda
Copy link
Collaborator

calda commented Sep 25, 2024

A similar example related to literals is:

let width: Double = 10
// vs
let width = Double(10)

@nicklockwood
Copy link
Owner

@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
Copy link
Author

jshier commented Sep 25, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants