[swift-evolution] Change 'for in' expression to for [] { (item) in ... }

Alex Blewitt alblue at apple.com
Fri Jul 28 11:31:19 CDT 2017


> On 28 Jul 2017, at 17:19, Kwanghoon Choi via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Hello
> 
> I found someone easy mistake using for in loop statement.
> 
> Ex)
> var i = 0
> for i in 0..<10 { }
> print(i)
> 
> And this user expected print(i) is “10”

The variable shadows any reference in the loop; and in addition, it's a constant value.

1> for i in 0..<10 { i+=1 } 
error: repl.swift:7:20: error: left side of mutating operator isn't mutable: 'i' is a 'let' constant
for i in 0..<10 { i+=1 }
                  ~^

> Many experienced swift developers doesn’t misunderstand like this. But always someone is new comers, and I think this expression make misunderstand easy too.
> 
> So why not like this?
> 
> var I = 0
> for 0..<10 { (i) in … }

You can already do this:

2> (0..<10).forEach { i in print(i) } 

Alex
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170728/f3c88241/attachment.html>


More information about the swift-evolution mailing list