[swift-evolution] For-loop revisited
Haravikk
swift-evolution at haravikk.me
Fri Feb 26 05:45:14 CST 2016
> On 26 Feb 2016, at 02:11, ted van gaalen via swift-evolution <swift-evolution at swift.org> wrote:
>
> for var n from 10 to 1 by -1 { } // looping backwards
This is currently possible with:
for var n in (1 … 10).reverse()
Which I find pretty simple personally, though being able to do 10 … 1 would be nice.
> for var n from -10 to 12 // here, the compiler assumes and increment value of 1, no "by" is needed.
The following already does this one as-is:
for var n in -10 … 12
> for var x: Float from -60.0 to 120 by 0.01 { } // this one has many iterations!
> for var d from 0.0 to abyss by -rate {}
These two definitely make more of a case for this. I’m not so sure about introducing new keywords for this though.
Personally I think that what we need is a more flexible type for the … and ..< operators that is simple two related values in any order, which Range could be instantiated from with more strict conditions (must be low to high) for things like grabbing arrays slices and so-on where the order matters. The basic non-Range would then be optimised for use with loops to give us the greater flexibility without having to do stuff like using .reverse().
Plus ideally the new type would be capable of representing a full range of values; I really don’t like that Range<UInt8> is currently limited to 0-254 inclusive, since the endIndex is always exclusive, but that’s another topic really.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160226/78922879/attachment.html>
More information about the swift-evolution
mailing list