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

Redundant conformance constraint 'Convertor': 'PersistedStorageConvertor' warnings #1

Closed
rob-secondstage opened this issue Oct 4, 2020 · 1 comment · Fixed by #2

Comments

@rob-secondstage
Copy link
Contributor

This project is great! I've been working on something similar for my app but was having trouble with the optionals. Your approach helped me a great deal!

The current code gives Redundant conformance constraint 'Convertor': 'PersistedStorageConvertor' warnings for all of the initializer extensions. In Swift 5.3, you could use Contextual Where Clauses to fix this, and as a side benefit, you only need one extension.

Such as:

extension Persisted {
    init(_ key: String, defaultValue: Exposed, storage: UserDefaults = .standard) where Convertor == IdentityStorageConvertor<NonOptionalExposed>, Exposed == NonOptionalExposed {
        self.init(key: key, defaultValue: defaultValue, valueConvertor: .init(), storage: storage)
    }
    init(_ key: String, storage: UserDefaults = .standard) where Convertor == IdentityStorageConvertor<NonOptionalExposed>, Exposed == NonOptionalExposed? {
        self.init(key: key, defaultValue: nil, valueConvertor: .init(), storage: storage)
    }
    init(_ key: String, defaultValue: Exposed, storage: UserDefaults = .standard)  where Convertor == RawRepresentableStorageConvertor<NonOptionalExposed>, NonOptionalExposed: RawRepresentable, Exposed == NonOptionalExposed {
        self.init(key: key, defaultValue: defaultValue, valueConvertor: RawRepresentableStorageConvertor(), storage: storage)
    }
    init(_ key: String, storage: UserDefaults = .standard) where Convertor == RawRepresentableStorageConvertor<NonOptionalExposed>, NonOptionalExposed: RawRepresentable, Exposed == NonOptionalExposed? {
        self.init(key: key, defaultValue: nil, valueConvertor: RawRepresentableStorageConvertor(), storage: storage)
    }
    // Note the different parameter name in the following: encodedDataKey vs unnamed. This is reqired since some Codable types
    // are also UserDefaultsPrimitive or RawRepresentable. We need a different key to be able to avoid ambiguity.
    init(encodedDataKey key: String, defaultValue: Exposed, storage: UserDefaults = .standard) where Convertor == CodableStorageConvertor<NonOptionalExposed>, Exposed == NonOptionalExposed {
        self.init(key: key, defaultValue: defaultValue, valueConvertor: CodableStorageConvertor(), storage: storage)
    }
    init(encodedDataKey key: String, storage: UserDefaults = .standard) where Convertor == CodableStorageConvertor<NonOptionalExposed>, Exposed == NonOptionalExposed? {
        self.init(key: key, defaultValue: nil, valueConvertor: CodableStorageConvertor(), storage: storage)
    }
}
@AndrewBennet
Copy link
Owner

Great to hear that the project has been useful! Yes, properly handling the optionals was a bit tricky; it wasn't at all obvious at first (to me) that having two generic arguments (one the unwrapped type) was the solution.

Thanks for raising this - I had noticed that I'd started getting some build warnings when using this in Reading List, but had not yet looked into it. I'd be happy to merge this if you raise it as a pull request; otherwise I can make this change myself soon.

rob-secondstage added a commit to rob-secondstage/PersistedPropertyWrapper that referenced this issue Oct 5, 2020
Changed from separate extension for each initializer to a single extension where each initializer uses a 'contextual where clause' to specify its constraints.
This removes the 'Redundant conformance constraint 'Convertor': 'PersistedStorageConvertor' warnings for the separate extensions.
Fixes AndrewBennet#1.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants