Not to doubt the usefulness of this proposal in general, but your example is 
subsumed by simply declaring 

var result = "\(gen.next()!)"

and proceeding with the rest of the example sans `once`.  I think you'll also 
have to address how this is any different from dispatch_once or a DSL over the 
same.

Cheers,

~Robert Widmann

2016/05/17 0:03、Nicholas Maccharoli via swift-evolution 
<[email protected]> のメッセージ:

> Hello Swift Evolution,
> 
> Its not uncommon to have to do a piece of work only once or on the first 
> iteration of 
> a loop.
> Take for example producing a comma separated string from an Array:
> 
>     var result = ""
> 
>     let values = [1, 2, 3, 4, 5]
> 
>     var gen = values.generate()
> 
> 
> 
>     if let first = gen.next() {
> 
>         result += "\(first)"
> 
>         while let value = gen.next() {
> 
> 
>             result += ", "
> 
>             result += "\(value)"
> 
>         }
> 
>     }
> 
> 
> 
> Since on the first iteration we want to skip putting a comma in front we use 
> an `if let` to grab the first element and then embed a `while let` inside the 
> `if let` to handle the rest.
> 
> 
> 
> Another way to do this could be using a bool to keep track of the first 
> iteration:
> 
> 
> 
>     var first = true
> 
>     while let value = gen.next() {
> 
>         if first {
> 
>             result += "\(value)"
> 
>             first = false
> 
>             continue
> 
>         } else {
> 
>             result += ", "
> 
>             result += "\(value)"
> 
>         }
> 
>     }
> 
> 
> 
> These approaches work, but I think there may be a way to do this with less 
> noise.
> 
> If there was a keyword to execute a block of code only on the first iteration 
> of a loop I think that would make code like this more concise.
> 
> If there was a keyword like `once` then the same thing could be achieved with 
> something like:
> 
> 
> 
>     while let value = gen.next() {
> 
>         once {
> 
>             result += "\(value)"
> 
>             continue
> 
>         }
> 
>         result += ", "
> 
>         result += "\(value)"
> 
>     }
> 
> 
> 
> How does it sound?
> 
> 
> 
> - Nick
> 
> _______________________________________________
> 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