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

Add conditional conformance to the @Element property wrapper to the ExpressibleBy___Literal Swift Standard Library protocols similar to the existing implementation for the @Attribute property wrapper #282

Open
thafner0 opened this issue May 26, 2024 · 0 comments

Comments

@thafner0
Copy link

At present to instantiate a property that is wrapped with the @Attribute property wrapper whose wrapped value conforms to one of the many ExpressibleBy(value type)Literal such as ExpressibleByStringLiteral:

struct TwoStrings: Equatable, Codable {
    @Element var string1: String
    @Attribute var string2: String
}

Currently in order to instantiate this type, one must do the following:

let twoStrings = TwoStrings(string1: Element.init("Hello"), string2: "Goodbye")

This is because the @Element property wrapper doesn't conform to the ExpressibleByStringLiteral, unlike the @Attribute property wrapper. My suggestion is to the conditional conformance to the ExpressibleByStringLiteral protocol whenever the Value type parameter also conforms to the ExpressibleByStringLiteral protocol. (This suggestion also applies for the other protocols that are of the format ExpressibleBy(value type)Literal protocols such as ExpressibleByIntegerLiteral and ExpressibleByBooleanLiteral). The way that this would be done would be virtually identical to the way this is implemented in the @Attribute property wrapper. (As such, one could argue that this should be implemented with the definition of a protocol that satisfies its own requirements. This protocol could then be inherited by the @Attribute and @Element property wrappers in order to avoid repetitive code).

Regardless of the exact implementation, the adoption of this suggestion would permit the instantiation of the example type as follows (note the similarities between the two properties, despite their differing implementation on the XML side):

let twoStrings = TwoStrings(string1: "Hello", string2: "Goodbye")
@thafner0 thafner0 changed the title Add conditional conformance to the Element property wrapper to the ExpressibleBy___Literal Swift Standard Library protocols similar to the existing implementation for the Attribute property wrapper Add conditional conformance to the Element property wrapper to the ExpressibleBy___Literal Swift Standard Library protocols similar to the existing implementation for the Attribute property wrapper May 26, 2024
@thafner0 thafner0 changed the title Add conditional conformance to the Element property wrapper to the ExpressibleBy___Literal Swift Standard Library protocols similar to the existing implementation for the Attribute property wrapper Add conditional conformance to the @Element property wrapper to the ExpressibleBy___Literal Swift Standard Library protocols similar to the existing implementation for the @Attribute property wrapper May 26, 2024
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

1 participant