[Pitch #3] Property wrappers (formerly known as Property Delegates)

Can I still post a question in this thread?

Regarding synthesized Decodable, I am trying to find a way to make an omitted key acceptable.

The following type will decode the JSON "{}" successfully:

struct OmitTest: Decodable {
    var val: String?
}

let val = try! JSONDecoder().decode(OmitTest.self, from: #"{}"#.data(using: .utf8)!)

However, I cannot think of a way to retain that synthesized behavior while adding a wrapper to the property val.

Given the trivial Property Wrapper

@_propertyWrapper
struct Omittable<T: Decodable>: Decodable {
    var value: T?

    init(from decoder: Decoder) throws {
        var container = try decoder.unkeyedContainer()

        value = try container.decode(T.self)
    }
}

The following code exhibits the behavior I am looking for (without using the property wrapper as a property wrapper)

struct OmitTest: Decodable {
    var val: Omittable<String?>?
}

However, I cannot find a way to retain that decoding behavior when using it as an attribute

struct OmitTest: Decodable {
    @Omittable var val: String?
}

Is the above the intended behavior of synthesized decoding?

2 Likes