[swift-evolution] A (better) Swift Equivalent For The Classical For-Loop With Numeric Scalars

Biala bialata at yahoo.com
Thu Mar 24 00:56:21 CDT 2016


> I get this
> 
> var i:Int = 0
> for i in (0..<len)
> {
>    //do something
> }
> 
> ....
> 
> for i in (0..<len)
> {
>    if something is true
>    {
>        break
>    }
> }
> 
> use i ??
> What is i now :)  !!!!

Yes and it is extremly confusing sintax..... and you may create such bug without even notice just by changing the old C style for cycles. 
I am still new to SWIFT (2 months). Here is what I thing
1. Breaking existing code meens that swift is still in beta and one should always think twice before investing in big project.
2. swift has no clear vision of "for" cycles. All cycles resemble "while" cycle when compiled to CPU instructions and can be implemented with while loop. Pascal had a limited for cycles just for easy enumeration. C on the other hand has much more capable for cycles that suited many cases. From what I read there is no clear idea about "for" in swift. They have to write down the scenarios they want to cover and and offer ONE syntax for them all. Having a different syntax for different C scenarios is confusing - better leave the C syntax. 
3. swift has hidden function calls like C# - that affects the code performance in an unexpected way. Function calls are expensive in ARM architecture and they should be avoided in loops. So this leads to the arrays. Swift is slow beacause reading and changing array members is slow and that is what usually is made in loops. There should be no hiden function calls there. I may suggest using UnsafeMuttablePointer to create a new fast array  class or struct and add it into the language for everyone to use.
4. writing many things in one line is BAD in million lines projects. It is called "compressed logic" - leads to bugs, and slow code maintenance. Swift closers are becoming really messy at this point so some rules against compressed logic are needed.
5. There are many great things in swift that I have not seen anywhere else like swift functions, interaction with C code, dictionaries and more and more. So I have a strong believe in the swift development team .
 

    On Thursday, March 24, 2016 5:59 AM, Brent Royal-Gordon <brent at architechies.com> wrote:
 

 > I get this
> 
> var i:Int = 0
> for i in (0..<len)
> {
>    //do something
> }
> 
> ....
> 
> for i in (0..<len)
> {
>    if something is true
>    {
>        break
>    }
> }
> 
> use i ??
> What is i now :)  !!!!

The outer `i` is 0 because it was never used. The expression `for i in...` implicitly declares a new `i`.

-- 
Brent Royal-Gordon
Architechies


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


More information about the swift-evolution mailing list