SE-0231 — Optional iteration

Thanks for so thoroughly investigating your own codebase for this! As review manager, I would be interested in seeing more hard evidence of this sort from anyone else willing to spend some time mining their own private Swift codebases (I can look at Github well enough myself, though in our experience, open-source repos have not always been an accurate representation of all code). If you (or others) have time to refine your methodology and get better results, I'd also appreciate that.

One concern that comes to my mind with this sort of approach, though, is whether the existing language design, by imposing this impediment, forces developers to code around it. By not providing a direct means to iterate over optional collections, a developer could be pushed to change their coding approach more drastically in an attempt to avoid having to do so, perhaps by handling optionals more eagerly elsewhere. One could argue that's a good thing, but it might also obscure how much of an immediate frustration this really is if you try to divine it from the code. You mention anecdotally that it hasn't been a problem for yourself; do you happen to know whether your coworkers feel the same?



At a meta-level, I feel like we've had a good and thorough discussion of both whether iterating sequences wrapped in Optional is an important problem to solve, and many approaches to how to solve it if so. I'll try to summarize, and please reply if I've left your position out:

  • We could take the proposed for? syntax as is
  • We could use some other special case syntax, such as for x in? optional
  • We could include for loops in optional chains, so that for x in optional?.sequence works. Some people, including the proposal as an alternative, have suggested "put a ? after the sequence", for x in optional?, which can be seen as the degenerate case of optional-chaining the for loop. This has also led to discussion about whether optional chaining ought to be generalized in more ways, such as to arguments, binary operators, or other statement forms, particularly if optionalBool?.
  • We could allow for loops to accept multiple statement conditions, in the manner of if and while, using the final in form as the sequence to be iterated, for let xs = optional, x in xs
  • We could make for loops implicitly accept Optional with a Wrapped sequence.
  • We could leave the language as is, and make a library change to address this problem:
    • Introducing an orEmpty method on Sequence that produces a wrapper
    • Making Optional conform to Sequence when it has a Wrapped sequence
    • Continue to promote x ?? [] (or equivalent empty literal syntax) as the idiom for handling optionals containing sequences

Note that by listing these out, I'm trying only to summarize and confirm that these positions have been heard, not implying anything about the merits of any particular solution, or whether making any change is the desired outcome of this review. Personally, I'd like to see deeper analysis of the "Is this problem important to solve" angle, and doing more data analysis like what @algal has provided would be very interesting.

19 Likes