Another try at allowing optional iteration

There has just been a decision for a proposal in problem space of how to deal with Optionals, and although it was rejected, it may still have some impact on Swift.

Although this is taken out of context, I think this sentence captures the spirit of the rationale:
Force unwrapping won't be banned, but it should be easier to use the alternatives.

Now, here we have a situation where force unwrapping is extremely easy and convenient, and the "better" alternatives are either much more cumbersome (if let), less powerful (?.forEach) or not universal and/or potentially less performant (?? []).
At the same time, there is an extremely obvious solution ("if there's nothing to iterate, don't iterate") which just has no syntax to be expressed.

I don't like treating a nil-value as an empty sequence, but like in many other cases where we make Optionals more convenient, a single question mark could be enough to solve the problem.

2 Likes