[swift-evolution] For-loop revisited

Maximilian Hünenberger m.huenenberger at me.com
Tue Mar 8 11:29:51 CST 2016


I'm a bit late to the discussion but how about something like this:

for x in (0..<5).by(0.3) { ... }
// or
for x in (0..<5).strideBy(0.3) { ... }
// or
for x in stride(0..<5, by: 0.3) { ... }

Greetings
- Maximilian

> Am 27.02.2016 um 00:31 schrieb Ted F.A. van Gaalen via swift-evolution <swift-evolution at swift.org>:
> 
> Thanks for clarifying, David
> 
> However, to me, it still remains awkward and indirect to 
> use collections in cases where they are not necessary at all. 
> 
> About reserved words: 
> The sub-keywords  (‘from’, ‘to’, and ‘by’)  
> are context dependent and will almost certainly 
> not be used on their own, rather always together
> with a primary keyword.
> 
> An advanced compiler should be able to
> determine from the context of a statement 
> if words used in that statement act as keywords or not! 
> 
> For instance the PL/1 language from < 1970 , far ahead of its time,  **1**
> in spite of being very extensive, has no reserved words at all.
> 
> Most people will not use reserved words as identifiers because this is confusing.  
> 
> I will write a Swift proposal next week for
> for .. from .. to.. [by.. ] ,
> in spite of all of its functionality being 
> already present in the classical for ;; loop.  
> 
> I still cannot find a reason why it should be removed.
> As with any other language construct,
> it really depends on how one uses it.
> Like with any other language elements in Swift,
> it’s very easy to create a mess, no one can prevent this.
> 
> Of course, I would use a  for..in.. 
> if dealing with a collection: using a 
> classic for ;;  for that is clearly not an advantage.
> 
> TedvG
> 
> 
> 
>   **1**    As a source of inspiration, one might find this interesting:
> https://en.wikibooks.org/wiki/Software_Engineers_Handbook/Language_Dictionary/PLI#Looping_Statements
> 
> Ted
> 
>>> On 26.02.2016, at 21:52, David Waite <david at alkaline-solutions.com> wrote:
>>> 
>>> 
>>> On Feb 26, 2016, at 9:07 AM, Ted F.A. van Gaalen via swift-evolution <swift-evolution at swift.org> wrote:
>>> 
>>>>> Does .stride(), which in the end uses a descendant of SequenceType, just calculate a new value each time   for..in..   uses .next() on this collection?
>> 
>> this.
>> 
>>>    for  x from xmin to xmax by xstep  { }
>>> 
>>>    for x from xmax to xmin by -xstep  { } 
>>> 
>>>    for apple from 1 to applesInTruck  { }
>>> 
>>> No need for collections in these cases,
>> 
>> As the thread for removal of C-style for showed in benchmarks, using a range or stride does not have a performance impact under optimization. Such new syntax would need to stand on its own as a second alternative to using ranges/strides. 
>> 
>> Considering that it would require reserving three new keywords (‘from’, ‘to’, and ‘by’) this will be a hard argument to make.
>> 
>> -DW
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160308/2306135d/attachment.html>


More information about the swift-evolution mailing list