thoughtbot / Argo

Functional JSON parsing library for Swift
https://thoughtbot.com
MIT License
3.49k stars 198 forks source link

[RFC] Remove optional decoding as an explicit operation #490

Open gfontenot opened 6 years ago

gfontenot commented 6 years ago

Previously, we've used a special operator (<|?) for denoting that we were decoding to an optional property. This operator had special behavior that meant it would only fail if the decoding operation itself failed, but would still succeed if the key was missing.

I think that ultimately, this was a confusing distinction. Although it was nice and concise, it made it difficult to reason about some transformations (like flatMapping into a secondary transformation), and it also meant we needed to double the surface area of the API. The special behavior around when we failed the decoding was also potentially confusing and could lead to unexpected results for people that weren't completely familiar with the distinction between how we handled various decoding failures internally.

Additionally, we've had another solution to this problem for nearly the entire time this library has existed: Decoded.optional. This is a much simpler transformation, and it simply converts the Decoded to an Optional and then wraps it in .success.

It seems reasonable, then (especially with the additional API changes currently in flight) to reduce the surface area and the complexity of our API at the same time by removing the <|? operators in favor of explicitly wrapping our extraction operations in a call to .optional.

Note that this also means we can use conditional conformance to make conform Optional conform to Decoded directly.

gfontenot commented 6 years ago

It's possible that I'm proposing removing this as an implicit operation, now that I'm thinking about it.

gfontenot commented 6 years ago

Updated this for the new subscript syntax. Feels more like pushing peas around on the plate now than it did when it was replacing <|? but I still think that simplifying the behavior and reducing the API surface area is worth it. Would love to hear if anyone has opinions about this.