[swift-evolution] Feature proposal: Range operator with step

davesweeris at mac.com davesweeris at mac.com
Thu Apr 14 21:29:18 CDT 2016


“|” often means “with” in math, which is only one word off from “with step”.
func | <T: Strideable where T.Stride : IntegerType> (range: Range<T>, stride: T.Stride) -> IntegerStrideTo<T> {
    return IntegerStrideTo(_start: range.startIndex, end: range.endIndex, stride: stride)
}
var arr = [Int]()
for i in (0 ..< 10) | 2 {
    arr.append(i)
}
arr //[0,2,4,6,8]

I couldn’t figure out how to do it without the parens… everything I could think to try is determined to parse as `(0) ..< (10 | 2)`, so `arr` ends up equalling [0,1,2,3,4,5,6,7,8,9]. If operator precedence and associativity were per-function rather than per-op, it could be made to work without the ().

- Dave Sweeris

> On Apr 14, 2016, at 8:27 PM, Brent Royal-Gordon via swift-evolution <swift-evolution at swift.org> wrote:
> 
> That means we would need an expression along the lines of:
> 
> 	1..<10 by 2
> 
> Which could be used anywhere. Unfortunately, Swift does not allow word characters in identifiers, so `by` as an operator is a non-starter. I can't think of a non-letter operator for `by` that would make sense, so we're probably not going to go that route, either (but if you have a suggestion—preferably one backed by existing notation from, say, math—by all means suggest it).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160414/4f1eb9c1/attachment.html>


More information about the swift-evolution mailing list