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

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

This change would also make it so that for loops follow the same format as all other closures with variables.

The downside is that this would break a TON of code. I don’t think that the amount of code this breaks would be worth the consistency. And as Alex mentioned, it is possible through .forEach

···

On Jul 28, 2017, at 10:19 AM, Kwanghoon Choi via swift-evolution <swift-evolution@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
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Isn’t this exactly like forEach, which is already in the language?

···

On Jul 28, 2017, at 12:32 PM, Jacob Williams via swift-evolution <swift-evolution@swift.org> wrote:

This change would also make it so that for loops follow the same format as all other closures with variables.

The downside is that this would break a TON of code. I don’t think that the amount of code this breaks would be worth the consistency. And as Alex mentioned, it is possible through .forEach

On Jul 28, 2017, at 10:19 AM, Kwanghoon Choi via swift-evolution <swift-evolution@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
_______________________________________________
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

This is funny because this “expected behavior” is actually a heavily
criticized part of C behavior. I think this change would cause just as much
if not more confusion than it alleviates.

···

On Fri, Jul 28, 2017 at 12:19 PM, Kwanghoon Choi via swift-evolution < swift-evolution@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
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

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

···

On 28 Jul 2017, at 17:19, Kwanghoon Choi via swift-evolution <swift-evolution@swift.org> wrote:

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.

···

On 28 Jul 2017, at 17:19, Kwanghoon Choi via swift-evolution <swift-evolution@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

yes, swift already has forEach, but from my point of view, there is an
uncomfortable part, like '(0..<10).forEach{}'. like Range type to need wrap
it in parentheses for using forEach.

my suggestion is not replacing 'for in [] {}' to 'for [] {}'. Can not we
have both? Of course, by erasing one before, it gave an environment to make
cleaner code easier for developers like me.

···

2017-07-29 1:38 GMT+09:00 Robert Bennett <rltbennett@icloud.com>:

Isn’t this exactly like forEach, which is already in the language?

> On Jul 28, 2017, at 12:32 PM, Jacob Williams via swift-evolution < > swift-evolution@swift.org> wrote:
>
> This change would also make it so that for loops follow the same format
as all other closures with variables.
>
> The downside is that this would break a TON of code. I don’t think that
the amount of code this breaks would be worth the consistency. And as Alex
mentioned, it is possible through .forEach
>
>
>> On Jul 28, 2017, at 10:19 AM, Kwanghoon Choi via swift-evolution < > swift-evolution@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
>> _______________________________________________
>> 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

Thanks for all replies.

This is helpful for introduce why for in be that for in other peoples.

나의 iPhone에서 보냄

2017. 7. 29. 05:14 Taylor Swift <kelvin13ma@gmail.com> 작성:

···

This is funny because this “expected behavior” is actually a heavily criticized part of C behavior. I think this change would cause just as much if not more confusion than it alleviates.

On Fri, Jul 28, 2017 at 12:19 PM, Kwanghoon Choi via swift-evolution <swift-evolution@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
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Maybe a better change would be for every function not nested within another, all local variables (including the in-function name for parameters and the parameters/locals of nested functions) need to be uniquely named. (I think some obscure language I read about once does this.)

···

On Jul 28, 2017, at 12:19 PM, Kwanghoon Choi via swift-evolution <swift-evolution@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.


Daryle Walker
Mac, Internet, and Video Game Junkie
darylew AT mac DOT com

I simply can’t see changing the syntax of such a fundamental construct, especially when a suitable replacement, which accomplishes your desired goal of introducing no new variables outside (visually) of a pair of curly braces, already exists. The language’s development is well past the stage of the cosmetics of basic syntax.

···

On Jul 28, 2017, at 12:57 PM, Kwanghoon Choi <eyerama@gmail.com> wrote:

yes, swift already has forEach, but from my point of view, there is an uncomfortable part, like '(0..<10).forEach{}'. like Range type to need wrap it in parentheses for using forEach.

my suggestion is not replacing 'for in [] {}' to 'for [] {}'. Can not we have both? Of course, by erasing one before, it gave an environment to make cleaner code easier for developers like me.

2017-07-29 1:38 GMT+09:00 Robert Bennett <rltbennett@icloud.com>:

Isn’t this exactly like forEach, which is already in the language?

> On Jul 28, 2017, at 12:32 PM, Jacob Williams via swift-evolution <swift-evolution@swift.org> wrote:
>
> This change would also make it so that for loops follow the same format as all other closures with variables.
>
> The downside is that this would break a TON of code. I don’t think that the amount of code this breaks would be worth the consistency. And as Alex mentioned, it is possible through .forEach
>
>
>> On Jul 28, 2017, at 10:19 AM, Kwanghoon Choi via swift-evolution <swift-evolution@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
>> _______________________________________________
>> 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

Terms of Service

Privacy Policy

Cookie Policy