CoreOffice / XMLCoder

Easy XML parsing using Codable protocols in Swift
https://coreoffice.github.io/XMLCoder/
MIT License
793 stars 104 forks source link

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 2 months ago

thafner0 commented 2 months ago

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")