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

Move the required init from BackedDecodable to a static var or function #1

Closed
ABridoux opened this issue Mar 23, 2021 · 1 comment
Closed

Comments

@ABridoux
Copy link

Idea

Having to declare an empty init() might lead to a crash as mentioned in the Readme. It might be clearer to move this requirement to a static variable or function, which clear statement that it should not be used and is only a protocol requirement.

Examples

public protocol BackedDecodable: Decodable {

    /// Requirement for the `BackedProtocol` when decoding. Should not be used
    //// to instantiate a new value.
    static var emptyBacked: Self
   // or
   static var _emptyBacked: Self
   // or
   static func _emptyBacked() -> Self
}

So the example User in Readme could become:

struct User: BackedDecodable {

    private init() {}
    static func _emptyBack() -> Self { User() }

    init(id: String, firstName: String, lastName: String) {
        self.$id = id
        self.$firstName = firstName
        self.$lastName = lastName
    }
    
    @Backed("uuid")
    var id: String

    @Backed(Path("attributes", "first_name"))
    var firstName: String
    
    @Backed(Path("attributes", "last_name"))
    var lastName: String
}

Miscellaneous

It's clear in the examples that this would require to make BackedDecodable having an associated type, which has its complexity. Meanwhile, it does appear that it's not required in the library to use objects of types BackedDecodable so this might not be a problem.

@jegnux
Copy link
Owner

jegnux commented Mar 23, 2021

@ABridoux that's a good idea. I improved it a little bit by changing the required init() for a required init(_:DeferredDecoder) where DeferredDecoder can't be init out of BackedCodable module, guarantying this init can't be used by mistake.

You can check implementation details in c28a4e5

@jegnux jegnux closed this as completed Mar 23, 2021
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

No branches or pull requests

2 participants