You are a stupid programmer, you can't have that
Steven Schveighoffer
schveiguy at gmail.com
Tue Aug 10 00:56:20 UTC 2021
On 8/9/21 1:14 PM, IGotD- wrote:
> On Monday, 9 August 2021 at 16:27:28 UTC, Steven Schveighoffer wrote:
>>
>> However, I do know of cases that have gone too far. Like Swift
>> eliminating for loops -- that one stung.
>>
>
> Is this correct?
Yes.
> for loops with classical C syntax are removed but you
> can still have for loops over ranges and the x...y syntax just makes an
> integer range of your liking. This is similar to foreach (i; 0 .. 3) in D.
There are more uses for traditional `for` loops than just looping over
integers or ranges.
>
> It's just a syntax change and bugs with ranges is probably just as easy
> as with the old C syntax.
The use case I had, I needed to rewrite into a sequence. Hm... Let me
find the change:
```swift
for var i = 0; (gridOrigin.x + CGFloat(i) * subGridSpacing.width) *
scale < bounds.width; i += 1 {
...
```
I wrote a "for generator" sequence type that accepted a lambda for the
condition. It now looks like:
```swift
for i in forgen(0, condition: { (self.gridOrigin.x + CGFloat($0) *
self.subGridSpacing.width) * self.scale < self.bounds.width }){
...
```
This is just a hack, and not a full replacement. It takes an initial
value and an increment (by default 1), and only works with Int.
Honestly, it was a long time ago (heck, they probably removed more
features and this won't compile now). There might be better ways. And I
probably would have been fine with swift never having for loops. But to
start out with them, and then remove them, seemed unnecessary.
-Steve
More information about the Digitalmars-d
mailing list