SE-0231 — Optional iteration

The discussion has shown that always appending ?, as the alternative solution suggests, doesn't prove to be a reasonable rule in the general case, especially in relation to expressions that already acknowledge optional behavior:

for x in try? sequence.something() { ... }

for x in data as? [String] { ... }

So right now the most promising compiler-side solution is to only require ? where it isn't acknowledged, so:

for x in optionalSequence? { ... }  

for x in optionalSequence?.reversed() { ... }
1 Like
What is your evaluation of the proposal?

+0.5, for both bike shedding and importance reasons.

First, I wonder if another syntax would be more consistent with the language, for instance:

for person in let people = people { ... }

might be more consistent with the rest of the language.

Second, I wonder if this is really needed enough to complicate the grammar at all.

Is the problem being addressed significant enough to warrant a change to Swift?

To be honest, I don't think so.

I haven't run into this case, as I typically avoid optional except when needed. I don't have much data where an empty sequence and a missing sequence would have different meanings, so by the time I try to process data it would have some other mechanism to either skip processing the sequence, or have been converted to an empty sequence.

If a missing and empty sequence mean two different things, then people will generally be using optionality for a distinct processing path. If not - why bother have a missing sequence in your data model at all?

Does this proposal fit well with the feel and direction of Swift?

I would imagine in terms of simplifying control flow (focusing on the business logic than complex, nested conditional logic)

If you have used other languages or libraries with a similar feature, how do you feel that this proposal compares to those?

None

How much effort did you put into your review? A glance, a quick reading, or an in-depth study?

Argued in the original thread :slight_smile:

2 Likes

Thanks again to everyone who participated in this review discussion. The core team has decided to reject the proposal, but would like to consider a new proposal focused on the optional-chaining alternative.

2 Likes