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
Why should this rule be added? Share links to existing discussion about what
the community thinks about this.
Collections types, such as dictionaries, arrays, etc. should default to empty collections rather than optional collections. Using optional collections often means a double check of ensuring the collection is not nil, and checking the collection has members; while optional chaining can make this somewhat simple, it can still complicate the code. There's little point to a function returning an empty collection, rather than a nil collection, or defining a property as an optional collection rather than just providing it with a default empty collection.
Additionally, Apple strongly discourages the use of optional collections. This is a quote from Apple during an API review:
There's usually no meaningful distinction between an empty collection and "nothing", so it's somewhat rare to see collections as Optionals in Swift. If there's no particular reason to distinguish between [:] and nil, then the return value might as well drop the question mark.
Provide several examples of what would and wouldn't trigger violations.
Would trigger:
var foo: [Int]?
var bar: [String: Int]?
var moreFoo: Set<String>?
func doFoo()->[]? {}
func doBar()->[String: String]? {}
func someFunc(input: [String: String]?) {}
Would not trigger:
// properties can default to empty
var foo: [Int] = []
var bar: [String: Int] = [:]
var moreFoo: Set<String> = []
// functions should return empty collections rather than nil
func doFoo()->[] {}
func doBar()->[String: String] {}
// input parameters can default to empty collections rather than optionals
func someFunc(input: [String: String] = [:]) {}
// Optional collections can still come from nested collections:
var complexDic: [String: [String: Int]] = [:]
let item = complexDic["bob"]
// note: item is an optional collection here...
if let item = item {
....
}
Should the rule be configurable, if so what parameters should be configurable?
It's not likely there are any valid configurable options
Should the rule be opt-in or enabled by default? Why?
See README.md for guidelines on when to mark a
rule as opt-in.
I'd suggest the rule be enabled by default. To reference above, it's already rare to use optional collections in Swift, and there's usually no meaningful distinction between an empty collection and nil.
The text was updated successfully, but these errors were encountered:
Unfortunately the quote is not available in a public link. My company has been working with Apple, and this came up during a review of some the API we're working with them on. I can remove it from the request if that's a problem.... thx
New Issue Checklist
Rule Request
the community thinks about this.
Collections types, such as dictionaries, arrays, etc. should default to empty collections rather than optional collections. Using optional collections often means a double check of ensuring the collection is not nil, and checking the collection has members; while optional chaining can make this somewhat simple, it can still complicate the code. There's little point to a function returning an empty collection, rather than a nil collection, or defining a property as an optional collection rather than just providing it with a default empty collection.
Additionally, Apple strongly discourages the use of optional collections. This is a quote from Apple during an API review:
Would trigger:
Would not trigger:
It's not likely there are any valid configurable options
See README.md for guidelines on when to mark a
rule as opt-in.
I'd suggest the rule be enabled by default. To reference above, it's already rare to use optional collections in Swift, and there's usually no meaningful distinction between an empty collection and nil.
The text was updated successfully, but these errors were encountered: