Anviking / Decodable

[Probably deprecated] Swift 2/3 JSON unmarshalling done (more) right
MIT License
1.04k stars 73 forks source link

Subscripts as alternative to operators #142

Open Anviking opened 7 years ago

Anviking commented 7 years ago

We would perhaps soon have the opportunity to introduce subscripts as an alternative to operators. Here are some hasty thoughts:

Pros:

Cons:

I'm pretty sure this would work:

let v: String? = root["a"]["b"]?["c"]
a and b are allow null, c does not allow null

If you want "c" to allow null, you'd have to mark it explicitly with another syntax. Perhaps with

["key".allow(.null, .missingKey, ...)]

There is a choice here whether to have all subscripts returning JSON structs, or whether to construct KeyPaths for as long as possible. I don't think there is an obvious advantage to any; try-catch-path-appending shenanigans are unavoidable, and being able to construct KeyPaths is nice.

With conditional protocol conformance, the need to generate overloads would go a way, so it would probably be easy to keep supporting the operator syntax.