Decodable icon indicating copy to clipboard operation
Decodable copied to clipboard

Subscripts as alternative to operators

Open Anviking opened this issue 8 years ago • 0 comments

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

Pros:

  • Can record A LOT of metadata using implicit parameters
  • Perhaps looks nicer

Cons:

  • Requires some redesign (although I've wanted to move in this direction anyways)
  • Requires JSON type, does not work with Any. In a lot of ways this makes sense though.

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.

Anviking avatar Mar 01 '17 06:03 Anviking