Persisting @propertyWrapper arguments after decoding

I'm doing some experiments with @propertyWrapper. I've got a propertyWrapper that adds a "comment" to a variable on type Testing , to be accessed with the projected value.

When I decode Testing however, the comment gets lost (the property wrapper is required to have Codable conformance & there isn't a way to reference the comment in the init(from: Decoder) ) Wondering if anyone knows a way of keeping that comment when decoding the Testing type.

@propertyWrapper
struct Comment<T>: Codable {
    private let comment: String
    var wrappedValue: T
    var projectedValue: String { self.comment }
    
    init(comment: String) {
        self.comment = comment
    }
    
    init(from decoder: Decoder) throws {
        // something here?
    }
    
    func encode(to encoder: Encoder) throws {
        // do nothing
    }
}

struct Testing: Codable {
    let bar: String
    
    @Comment(comment: "Some comment for this variable.") // Would like the `@Comment` property wrapper to have this string ("Some comment  
    var foo: String                                      // for this variable.") in it when an instance of Testing is decoded.
}

The example is contrived, but it gets the issue across.

There's currently no way to achieve that without actually writing comment into the coded storage.

1 Like