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

Haravikk swift-evolution at haravikk.me
Fri Jul 28 12:33:39 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”
> 
> 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 … }
> 
> I think this is more understandable for loop expression. 
> 
> Best Regards
> 
> - Jay Choi

While I sympathise with the intent, I think this is the wrong solution; it would be better to work towards eliminating some of the more common uses of shadowing (e.g- let x = x conditional binding) so that we could justify adding potentially accidental shadowing as a warning instead. Either that or just add shadowing as a warning by default, and maybe make a few exceptions where none will be raised.

Personally I avoid single character variable names except where they are part of a formula or such; makes for more verbose code, but that's not necessarily a bad thing, as it's usually clearer exactly what the variables are for as a result, at least when I can think of a good name.


More information about the swift-evolution mailing list