[swift-evolution] Extending declaration scope to condition for `repeat { } while ()`

Haravikk swift-evolution at haravikk.me
Mon Jul 18 17:16:31 CDT 2016

> On 18 Jul 2016, at 21:32, Sean Heber via swift-evolution <swift-evolution at swift.org> wrote:
> I’ve wanted this myself, too, so I’m generally +1, although I’ve also wondered if maybe this syntax should be changed somehow. I’ve not put a lot of thought into it, and this perhaps has come up before, but I sort of wonder.. would it make more sense to get rid of the trailing “while” entirely?
> Here’s what I’m thinking:
> This repeats forever - infinite loop:
> repeat {}
> And to get out of the infinite loop, you’d just use an if or guard the same way you might in any other loop in some cases:
> repeat {
>  let success = doSomething()
>  guard success else { break }
> }
> We could potentially even warn if you use a repeat {} without there being a break inside the body somewhere.
> This way, we eliminate a special syntactical form (repeat/while has always felt weird in every brace-based language I’ve used) and just piggyback on the existing break/continue/guard/if mechanisms that are already there and common. Then we also don’t need to have a special “weird” rule where the scope of variables change magically for repeat/while.
> l8r
> Sean

That's a very interesting alternative, especially with a warning when there's no break (or return), and it would be clearer about the scope. Actually now I think about it, the repeat/while is a little odd in Swift since the while condition doesn't require parenthesis, which is great for a regular while or if condition because the braces give it structure, but that's not quite the case with the repeat/while.

I'm fine either way, but this should be explored as well; guard in particular goes well this style of repeat syntax. More interestingly though is that this would actually allow us to write while loops as:

	repeat {
		guard someCondition() else { break }

while would essentially just become a shorthand.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160718/b20b6691/attachment.html>

More information about the swift-evolution mailing list