[Pre-RFC]: Generator integration with for loops

I’ve been thinking about this aswell. Some things I’d like to add:

We should support else blocks (or whatever we want to call them. I prefer then) and allow them to take an argument. This argument is the final result that the generator yielded.

let foo: u32 = for x in wow {
    ...
} then y {
    ...
}

The then block allows you to map the final result of the generator. If the generator returns () then we can skip naming the argument (ie. just have then { .. }).

The Generator trait should also be extended to take a resume argument. If this happens then we need a way to pass the argument when iterating the for loop. For this we can use continue

let gen: impl Generator<Resume=u32>;
for x in gen {
    ...
    continue 123;
}

If the generator resumes with anything other than () then the continue is mandatory at the end of every path in the for-loop body. If the generator does resume with () then we can continue to use continue with no argument, or to omit it entirely and the end of the body. This makes the usage of continue resemble the usage of break and return with regard to ().

5 Likes