[swift-evolution] Discussion: Move range (..., ..<) to a range() function

Kyle Bashour kylebshr at gmail.com
Tue Dec 8 13:01:00 CST 2015


Based on much of the discussion regarding removing C-style for-loops, I'd
like to propose a discussion on revamping how range works in Swift. The
lack of a reverse range operator and the fact and the range operator and
stride() seem to do a lot of the same work have made me wonder why there
isn't merely a range() function, as in Python.

I believe this would be easier for newcomers to learn, remove the need for
stride() (though there are probably use cases for stride() I don't know
about, I haven't used it too much), and actually be more clear than ..< and
...


Here are some examples of how it could work:

range(10) // equivalent to 0..<10
range(-1, to: 10) // equivalent to -1..<10
range(10, through: 0) equivalent to (0...10).reverse()
range(0, through: 10, by: 2) // equivalent to 0.stride(through: 10, by: 2)

Or, to avoid a global function, .range() should probably be a function like
stride, but with more features (equivalent to above)

10.range()
-1.range(to: 10)
10.range(through: 0)
0.range(through: 10, by: 2)

Would love thoughts on why this is good or bad, and if it's worth creating
an actual proposal.

Regards,

Kyle
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151208/537893cb/attachment.html>


More information about the swift-evolution mailing list