Skip to content

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

@thafner0

Description

@thafner0

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions