[swift-dev] C-style For Loops

Dave Abrahams dabrahams at apple.com
Sat Dec 19 17:04:08 CST 2015


> On Dec 19, 2015, at 2:24 PM, Brent Royal-Gordon <brent at architechies.com> wrote:
> 
>>> One practical reason is that interval.start must always be before interval.end, so this makes striding in reverse difficult.
>> 
>> We could declare that negative strides cause us to start at the end rather than at the start.
> 
> I've noticed in the past that the Swift standard library does not like to branch on sign.

Almost.  We simply don't like to branch :-)

> Of course, you could decide you just don't care in this particular case.

I'm willing to bet that in the vast majority of use cases, the sign of the stride can be known at compile-time and the branch can be eliminated.

>> None of those syntaxes fly for me, I'm afraid.  They all look like line noise (and some have precedence problems); I much prefer requiring a set of parentheses.
> 
> Even the free-function syntax?

I personally liked the original free-function syntax—"stride(from: -50, to: 50, by: 9)".  When we got protocol extensions, we decided we'd prefer methods everywhere except in a few special cases <https://swift.org/documentation/api-design-guidelines.html#general-conventions>, and this clearly falls outside those criteria.  Then there are also people who feel very strongly that we should avoid exposing an argument label on the first argument except in a few special cases <https://swift.org/documentation/api-design-guidelines.html#parameters>.

My first reaction to "stride(-60…60, by: 30)" is that you need some preposition between the verb and the first argument, though now that you ask again I can see reading "a...b" as "from a through b".  

I view the syntactic unification of -60...60 and (-60...60).by(30) as a big win, and it doesn't conflict with any of our API guidelines, which is another plus. 

>>>   for lat: CGFloat in stride(-60…60, by: 30) {
>>>        print(lat)
>>>  }
> 
> One more alternative, which reads sort of backwards but has clear precedence, no line noise, and no free functions:
> 
> 	for lat: CGFloat in 30.strideOver(-60...60) {
> 		...
> 	}

Others may differ, but asking 30 to stride over an interval really doesn't work for me, conceptually.
 
-Dave



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-dev/attachments/20151219/ba234850/attachment.html>


More information about the swift-dev mailing list