sindresorhus / Defaults

💾 Swifty and modern UserDefaults
https://swiftpackageindex.com/sindresorhus/Defaults/documentation/defaults
MIT License
2.1k stars 121 forks source link

Prevent using the initializer with `default` parameter for optional values #122

Open sindresorhus opened 2 years ago

sindresorhus commented 2 years ago

There's no way to prevent it today, but maybe when Swift supports macros or constant expressions.

I have seen many users do:

static let interval = Key<Double?>("interval", default: nil)

The default parameter is moot in this case.

Or even worse:

static let interval = Key<Double?>("interval", default: 4)

Which has the incorrect behavior by never allowing the value to be set to nil. (The correct solution to this is #54)

sindresorhus commented 1 year ago

I wonder if we could use a macro to detect this and issue a warning.