[OT] Swift removing minor features to piss me off

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Mon May 2 07:32:30 PDT 2016


On 5/2/16 6:55 AM, ag0aep6g wrote:
> On 02.05.2016 09:45, Jacob Carlborg wrote:
>> Does it matter? I thought the idea was to get the same behavior not
>> explicitly a range.
>
> default0 said that D's ranges would be more awkward than a for loop. I
> think D's iota is fine.
>
> D's special syntax is even nicer, but it's a language thing. And
> apparently Swift is cutting down on syntax, not adding. Something like
> iota is probably doable in a library. I don't know Swift, though.

Swift has both builtin syntax and library mechanism similar to iota:

for i in 0..<5 // [0 to 5)
for i in 0...5 // [0 to 5]
for i in 0.stride(to: 5, by: 1) // [0 to 5)
for i in 0.stride(through: 5, by: 1) // [0 to 5]

You can also hack with where expressions the builtin range syntax to 
skip values like iota, but I'm almost positive this doesn't perform 
well, or at least won't in complex situations:

for i in 0..<10 where i % 2 == 0 // even numbers less than 10

I think for-in (swift) and foreach (D) are both fantastic for iterating 
a straight range or pre-determined sequence. I would never use for loops 
for a straightforward index progression (and ideally, I would ignore the 
index if at all possible).

However, there are cases where I pull out the for loop because the 
ending condition is complex, or strangely related to the index variable, 
or the increments aren't regular. For loops have so much power in such a 
succinct syntax, I can't understand why anyone would shun them as taboo 
or error prone. Seems like it's more a philosophical decision than 
practical or helpful.

And anyone who says "bleh, you can just use a while loop if you need 
that" I want to beat with a semi-colon over the head.

-Steve


More information about the Digitalmars-d mailing list