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



> On Jul 18, 2016, at 12:52 PM, Braeden Profile via swift-evolution 
> <[email protected]> wrote:
> 
> Good morning, Swift community!
> 
> I’ve come across a situation a number of times where I write code that has to 
> try something one or more times using a `repeat…while` loop and the condition 
> relies upon variables that should be declared within the scope of the loop.
> 
> repeat
> {
>       let success = doSomething()
> }
> while !success
> 
> The compiler unnecessarily prohibits this:  “Use of unresolved identifier 
> four.”  In this simple case, we can write:
> 
> repeat
> { }
> while !doSomething()
> 
> But in a more complex situation, we are forced to write:
> 
> var success: Bool
> repeat
> {
>       success = doSomething()
> }
> while !success
> 
> 
> We could change this so that the declarations within the top level scope of 
> the loop are accessible from the condition.
> 
> Thanks for reading my first post to the Swift discussion board!
> —Braeden
> _______________________________________________
> swift-evolution mailing list
> [email protected]
> https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to