Studying the docs and implementations, my understanding is that Decoders are immutable value type objects. That means that it should be safe to make copies of them. I'd like to extract the Decoder instance so that I can make some more convenient top-level functions.
Extracting it is easy (and could be made even easier with a tiny change to stdlib). The question is whether this is safe (and whether Decoder promises enough that I can expect it to stay safe).
extension JSONDecoder {
private struct DecoderCloner: Decodable {
var decoder: Decoder
init(from decoder: Decoder) throws {
self.decoder = decoder
}
}
func decoder(for data: Data) throws -> Decoder {
try decode(DecoderCloner.self, from: data).decoder
}
}
With that, I can create more powerful versions of Decodable that pass parameters (as with DecodingConfiguration). I currently have to create top-level wrapper objects to get the process started, just to get a Decoder.
Is there a reason this would be unwise?