[swift-evolution] C-style For Loops

Erica Sadun erica at ericasadun.com
Fri Dec 4 12:56:54 CST 2015


I honestly have no idea where to go next with this. Here's my starting point: https://gist.github.com/erica/cf064a34f8891fb7a4a8

Advice welcome.

-- E, inexperienced at this stuff


> On Dec 3, 2015, at 11:02 PM, Kevin Ballard <kevin at sb.org> wrote:
> 
> A straight translation using UnsafePointer without a C-style for loop
> would look like
> 
> func blk_xor(dst: UnsafeMutablePointer<CChar>, src:
> UnsafePointer<CChar>, len: Int) -> UnsafeMutablePointer<CChar> {
>    for i in 0..<len {
>        dst[i] ^= src[i]
>    }
>    return dst
> }
> 
> -Kevin Ballard
> 
> On Thu, Dec 3, 2015, at 09:47 PM, ChanMaxthon wrote:
>> How about this C code when rewritten in Swift:
>> 
>> char *blk_xor(char *dst, const char *src, size_t len)
>> {
>>  const char *sp = src;
>>  for (char *dp = dst; sp - src < len; sp++, dp++)
>>    *dp ^= *sp;
>>  return dst;
>> }
>> 
>> Sent from my iPhone
>> 
>>> On Dec 4, 2015, at 13:35, Sean Heber <sean at fifthace.com> wrote:
>>> 
>>> I found a couple of cases of them in my codebase, but they were trivially transformed into “proper” Swift-style for loops that look better anyway. If it were a vote, I’d vote for eliminating C-style.
>>> 
>>> l8r
>>> Sean
>>> 
>>> 
>>>> On Dec 3, 2015, at 6:52 PM, Kevin Ballard <kevin at sb.org> wrote:
>>>> 
>>>> Every time I've tried to use a C-style for loop, I've ended up switching to a while loop because my iteration variable ended up having the wrong type (e.g. having an optional type when the value must be non-optional for the body to execute). The Postmates codebase contains no instances of C-style for loops in Swift.
>>>> 
>>>> -Kevin Ballard
>>>> 
>>>>> On Thu, Dec 3, 2015, at 04:50 PM, Eric Chamberlain wrote:
>>>>> We’ve developed a number of Swift apps for various clients over the past year and have not needed C style for loops either.
>>>>> 
>>>>> 
>>>>> --
>>>>> Eric Chamberlain, Lead Architect - iOS
>>>>> ArcTouch - App Development Studio
>>>>> 
>>>>> Custom apps for world-class brands and the Fortune 500
>>>>> arctouch.com/work | arctouch.com/blog
>>>>> 
>>>>> 
>>>>> 
>>>>> 
>>>>>> On Dec 3, 2015, at 3:46 PM, Andy Matuschak <andy at andymatuschak.org> wrote:
>>>>>> 
>>>>>> Just checked; ditto Khan Academy.
>>>>>> 
>>>>>>> On Dec 3, 2015, at 3:43 PM, Keith Smiley <keithbsmiley at gmail.com> wrote:
>>>>>>> 
>>>>>>> For what it's worth we don't have a single C style for loop in the Lyft
>>>>>>> codebase.
>>>>>>> 
>>>>>>> --
>>>>>>> Keith Smiley
>>>>>>> 
>>>>>>>> On 12/03, Douglas Gregor wrote:
>>>>>>>> 
>>>>>>>>> On Dec 3, 2015, at 3:32 PM, Erica Sadun <erica at ericasadun.com> wrote:
>>>>>>>>> 
>>>>>>>>> Does Swift still needs C-style for loops with conditions and incrementers? 
>>>>>>>>> 
>>>>>>>>> <Screen Shot 2015-12-03 at 4.30.15 PM.png>
>>>>>>>>> 
>>>>>>>>> More Swift-like construction is already available with for-in-statements and stride. 
>>>>>>>>> This would naturally starve the most common point for -- and ++ operators as well.
>>>>>>>> 
>>>>>>>> My intuition *completely* agrees that Swift no longer needs C-style for loops. We have richer, better-structured looping and functional algorithms. That said, one bit of data I’d like to see is how often C-style for loops are actually used in Swift. It’s something a quick crawl through Swift sources on GitHub could establish. If the feature feels anachronistic and is rarely used, it’s a good candidate for removal.
>>>>>>>> 
>>>>>>>> - Doug
>>>>>>> 
>>>>>>>> _______________________________________________
>>>>>>>> swift-evolution mailing list
>>>>>>>> swift-evolution at swift.org
>>>>>>>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>>>>>> 
>>>>>>> _______________________________________________
>>>>>>> swift-evolution mailing list
>>>>>>> swift-evolution at swift.org
>>>>>>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>>>>> 
>>>>>> _______________________________________________
>>>>>> swift-evolution mailing list
>>>>>> swift-evolution at swift.org
>>>>>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>>>> 
>>>>> _______________________________________________
>>>>> swift-evolution mailing list
>>>>> swift-evolution at swift.org
>>>>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>>> 
>>>> 
>>>> _______________________________________________
>>>> swift-evolution mailing list
>>>> swift-evolution at swift.org
>>>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>> 
>>> _______________________________________________
>>> swift-evolution mailing list
>>> swift-evolution at swift.org
>>> https://lists.swift.org/mailman/listinfo/swift-evolution
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution



More information about the swift-evolution mailing list