sunshinejr / SwiftyUserDefaults

Modern Swift API for NSUserDefaults
http://radex.io/swift/nsuserdefaults/static
MIT License
4.85k stars 365 forks source link

public extension on Optional #236

Open d4rkd3v1l opened 4 years ago

d4rkd3v1l commented 4 years ago

I'm a bit concerned about defining such a general/global extension public in a pod:

extension Optional: OptionalType, OptionalTypeCheck {
    public static var empty: Optional {
        return nil
    }
}

We have an extension on String:

extension String {
    static let empty: String = ""
}

Now when comparing an optional String to .empty, this will use your extension instead of our own extension on String. That really was no fun debugging -.-

Anyway, we can (and maybe even should) fix this on our side by writing our own extension specifically for optional Strings, or useString.empty explicitly, and so on. But in my opinion this is really error prone in such circumstances and can lead to quite unexpected behaviour.

sunshinejr commented 4 years ago

Hey @d4rkd3v1l - thank you for the issue, this is a very good suggestion. We should remove it if possible (not sure why is OptionalType a public protocol, but this is why we expose these properties).

RomanPodymov commented 3 years ago

Hello @d4rkd3v1l OptionalType is using in the public extensions in DefaultsKey.swift and Defaults+Subscripts.swift. As for me, you should refactor you code with myOptionalString == String.empty or any other solution.