Proposal: Remove the underscore and `in` for `for` loop


(Diego Barros) #1

When you want a simple `for` loop, for example:

for _ in 1...10 {

// do something 10 times

}

Clean-up and simplify the syntax by removing the superfluous underscore and
`in`:

for 1...10 {

// do something 10 times

}

-- diego


(Joe Bell) #2

+1, having to add an underscore in a X.times loop always feels contrived.

···

On Fri, Jul 1, 2016 at 2:38 AM, Diego Barros via swift-evolution < swift-evolution@swift.org> wrote:

When you want a simple `for` loop, for example:

for _ in 1...10 {

// do something 10 times

}

Clean-up and simplify the syntax by removing the superfluous underscore
and `in`:

for 1...10 {

// do something 10 times

}

-- diego

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--
Joseph Bell
http://dev.iachieved.it/iachievedit/
@iachievedit


(Sean Heber) #3

Minor, but I think I'd be onboard as well. Once proposed (and assuming it was approved), this would likely be deferred until post Swift 3 since it’s additive.

l8r
Sean

···

On Jul 1, 2016, at 2:38 AM, Diego Barros via swift-evolution <swift-evolution@swift.org> wrote:

When you want a simple `for` loop, for example:

for _ in 1...10 {

// do something 10 times

}

Clean-up and simplify the syntax by removing the superfluous underscore and `in`:

for 1...10 {

// do something 10 times

}

-- diego
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Adrian Zubarev) #4

Additive idea, but +1 for looping on ranges without the need on an index.

···

--
Adrian Zubarev
Sent with Airmail

Am 1. Juli 2016 um 09:39:23, Diego Barros via swift-evolution (swift-evolution@swift.org) schrieb:

When you want a simple `for` loop, for example:

for _ in 1...10 {

// do something 10 times

}

Clean-up and simplify the syntax by removing the superfluous underscore and `in`:

for 1...10 {

// do something 10 times

}

-- diego
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Anton Zhilin) #5

Diego Barros via swift-evolution <swift-evolution@...> writes:

for 1...10 {
// do something 10 times
}

Firstly, this should be delayed to post-Swift 3.
Secondly, I tend to vote for generalization and simplification of Swift
syntax. For example, I voted to replace 'default' with 'case _'. In this
case, I also prefer more general syntax. -1 from me.


(Tino) #6

+1 on anything that removes underscores :wink:


(Chéyo Jiménez) #7

-1 this is why we have collection.forEach{}

(1...10).forEach {
// do something.
}

···

On Jul 1, 2016, at 12:38 AM, Diego Barros via swift-evolution <swift-evolution@swift.org> wrote:

When you want a simple `for` loop, for example:

for _ in 1...10 {

// do something 10 times

}

Clean-up and simplify the syntax by removing the superfluous underscore and `in`:

for 1...10 {

// do something 10 times

}

-- diego
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Karl) #8

-1. I like the underscore. If it turns out you do need the loop variable later, it's easy to see where to add it.

Karl

···

On Jul 1, 2016 at 9:38 AM, <Diego Barros via swift-evolution (mailto:swift-evolution@swift.org)> wrote:

When you want a simple `for` loop, for example:

for _ in 1...10 {

>
>
>
> // do something 10 times
>
>
>

}

Clean-up and simplify the syntax by removing the superfluous underscore and `in`:

for 1...10 {

>
>
>
> // do something 10 times
>
>
>

}

-- diego

_______________________________________________ swift-evolution mailing list swift-evolution@swift.org (mailto:swift-evolution@swift.org) https://lists.swift.org/mailman/listinfo/swift-evolution


(Haravikk) #9

Does this idea apply in the same way? While "for _ in foo" and "for foo" would be equivalent, usually the underscore is used to indicate intent to avoid mistakes, which is why we wouldn't to be able to just use "case" on its own to represent match all, as it could be a case statement that the user didn't finish, the underscore in this case makes it clear that yes, what the user wants to do is ignore the element(s) themselves.

However, unlike the switch case, for omitting the underscore to represent a mistake you'd have to:
Forget the underscore
Forget the "in" keyword
Use a variable that should have been shadowed but wasn't, resulting in a subtle error.
I specify number 3 because if you use a variable that doesn't exist it will produce an obvious error. While the three are possible, they seem highly unlikely in combination.

That said, there is an argument to be made that this change would be inconsistent with using a result:

  foo.someMethod() // warning, unused result
  let _ = foo.someMethod() // result explicitly ignored

I'd possibly argue that a sequence is implicitly meant to have its results used, so they should be explicitly ignored for consistency's sake.

So… I like the short-form, but I like consistency, so I think I've manoeuvred myself into being a +0.1 or so :wink:

···

On 2 Jul 2016, at 14:19, Anton Zhilin via swift-evolution <swift-evolution@swift.org> wrote:

Diego Barros via swift-evolution <swift-evolution@...> writes:

for 1...10 {
// do something 10 times
}

Firstly, this should be delayed to post-Swift 3.
Secondly, I tend to vote for generalization and simplification of Swift
syntax. For example, I voted to replace 'default' with 'case _'. In this
case, I also prefer more general syntax. -1 from me.


(Christopher Kornher) #10

-1. I like the underscore. If it turns out you do need the loop variable later, it's easy to see where to add it.

Karl

-1 I agree with this and doing something a specific number of times without accessing the iteration counter is an edge case in my experience. Creating another syntax to save 4 characters for an edge case is a poor benefit/complexity tradeoff. Ignoring with an underscore is universal in Swift and, to me at least, expressive and intuitive.

···

On Jul 4, 2016, at 2:39 PM, Karl Wagner via swift-evolution <swift-evolution@swift.org> wrote:

On Jul 1, 2016 at 9:38 AM, <Diego Barros via swift-evolution <mailto:swift-evolution@swift.org>> wrote:

When you want a simple `for` loop, for example:

for _ in 1...10 {

// do something 10 times

}

Clean-up and simplify the syntax by removing the superfluous underscore and `in`:

for 1...10 {

// do something 10 times

}

-- diego
_______________________________________________ swift-evolution mailing list swift-evolution@swift.org <mailto:swift-evolution@swift.org> https://lists.swift.org/mailman/listinfo/swift-evolution_______________________________________________

swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Charlie Monroe) #11

-1 this is why we have collection.forEach{}

(1...10).forEach {
// do something.
}

This is not equivalent since it doesn't allow you to break from the for loop.

···

On Jul 1, 2016, at 12:38 AM, Diego Barros via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

When you want a simple `for` loop, for example:

for _ in 1...10 {

// do something 10 times

}

Clean-up and simplify the syntax by removing the superfluous underscore and `in`:

for 1...10 {

// do something 10 times

}

-- diego
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Chéyo Jiménez) #12

How would you build a condition to break if you are ignoring each value ? Unless you are hard coding a condition in which case I would still argue that the proposed shorthand for is less clear than `for _ in` or forEach.

···

On Jul 4, 2016, at 9:29 PM, Charlie Monroe <charlie@charliemonroe.net> wrote:

-1 this is why we have collection.forEach{}

(1...10).forEach {
// do something.
}

This is not equivalent since it doesn't allow you to break from the for loop.

On Jul 1, 2016, at 12:38 AM, Diego Barros via swift-evolution <swift-evolution@swift.org> wrote:

When you want a simple `for` loop, for example:

for _ in 1...10 {

// do something 10 times

}

Clean-up and simplify the syntax by removing the superfluous underscore and `in`:

for 1...10 {

// do something 10 times

}

-- diego
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Charlie Monroe) #13

Example:

for 1...5 {
  guard let data = self.loadSomeData() else {
    /// Will try it several times and then report failure to the user.
    continue
  }
  
  // process data
  return
}

// Error, failed to load data even after retrying.

···

On Jul 5, 2016, at 9:23 AM, Jose Cheyo Jimenez <cheyo@masters3d.com> wrote:

How would you build a condition to break if you are ignoring each value ? Unless you are hard coding a condition in which case I would still argue that the proposed shorthand for is less clear than `for _ in` or forEach.

On Jul 4, 2016, at 9:29 PM, Charlie Monroe <charlie@charliemonroe.net <mailto:charlie@charliemonroe.net>> wrote:

-1 this is why we have collection.forEach{}

(1...10).forEach {
// do something.
}

This is not equivalent since it doesn't allow you to break from the for loop.

On Jul 1, 2016, at 12:38 AM, Diego Barros via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

When you want a simple `for` loop, for example:

for _ in 1...10 {

// do something 10 times

}

Clean-up and simplify the syntax by removing the superfluous underscore and `in`:

for 1...10 {

// do something 10 times

}

-- diego
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution


(Xiaodi Wu) #14

`continue` can be rewritten `return` inside `forEach`.

···

On Tue, Jul 5, 2016 at 02:30 Charlie Monroe via swift-evolution < swift-evolution@swift.org> wrote:

Example:

for 1...5 {
guard let data = self.loadSomeData() else {
/// Will try it several times and then report failure to the user.
continue
}
// process data
return
}

// Error, failed to load data even after retrying.

On Jul 5, 2016, at 9:23 AM, Jose Cheyo Jimenez <cheyo@masters3d.com> > wrote:

How would you build a condition to break if you are ignoring each value ?
Unless you are hard coding a condition in which case I would still argue
that the proposed shorthand for is less clear than `for _ in` or forEach.

On Jul 4, 2016, at 9:29 PM, Charlie Monroe <charlie@charliemonroe.net> > wrote:

-1 this is why we have collection.forEach{}

(1...10).forEach {
// do something.
}

This is not equivalent since it doesn't allow you to break from the for
loop.

On Jul 1, 2016, at 12:38 AM, Diego Barros via swift-evolution < > swift-evolution@swift.org> wrote:

When you want a simple `for` loop, for example:

for _ in 1...10 {

// do something 10 times

}

Clean-up and simplify the syntax by removing the superfluous underscore
and `in`:

for 1...10 {

// do something 10 times

}

-- diego

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Charlie Monroe) #15

Sorry, my example should have been the other way around:

var data: NSData!
for 1...5 {
  data = self.loadSomeData()
  if data != nil {
    break // *this* can't be done with .forEach
  }
  
  /// Try again in next iteration
}

if data == nil {
  return
}

/// Process data.

But yeah, it can be written the other way around to avoid break.

···

On Jul 5, 2016, at 6:13 PM, Xiaodi Wu <xiaodi.wu@gmail.com> wrote:

`continue` can be rewritten `return` inside `forEach`.
On Tue, Jul 5, 2016 at 02:30 Charlie Monroe via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:
Example:

for 1...5 {
  guard let data = self.loadSomeData() else {
    /// Will try it several times and then report failure to the user.
    continue
  }
  
  // process data
  return
}

// Error, failed to load data even after retrying.

On Jul 5, 2016, at 9:23 AM, Jose Cheyo Jimenez <cheyo@masters3d.com <mailto:cheyo@masters3d.com>> wrote:

How would you build a condition to break if you are ignoring each value ? Unless you are hard coding a condition in which case I would still argue that the proposed shorthand for is less clear than `for _ in` or forEach.

On Jul 4, 2016, at 9:29 PM, Charlie Monroe <charlie@charliemonroe.net <mailto:charlie@charliemonroe.net>> wrote:

-1 this is why we have collection.forEach{}

(1...10).forEach {
// do something.
}

This is not equivalent since it doesn't allow you to break from the for loop.

On Jul 1, 2016, at 12:38 AM, Diego Barros via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

When you want a simple `for` loop, for example:

for _ in 1...10 {

// do something 10 times

}

Clean-up and simplify the syntax by removing the superfluous underscore and `in`:

for 1...10 {

// do something 10 times

}

-- diego
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution