This is true indeed. Traversing over sets or similar would not be possible with a 'step' applied. To fix this issue, an error could be generated when trying to apply 'step' on a for loop that does not iterate over a 'Strideable'.
I do not think an operator like ..+ is intuitive nor will it make code easier to read or write. > Am 24.03.2016 um 09:40 schrieb Xiaodi Wu <[email protected]>: > > If it's parentheses you want to avoid, you don't need a keyword to do that. > For example, I can make a stride operator `..+` in four lines like so: > > ``` > infix operator ..+ { associativity left precedence 134 } > func ..+ <Element: Strideable>(left: Range<Element>, right: Element.Stride) > -> StrideTo<Element> { > return left.startIndex.stride(to: left.endIndex, by: right) > } > > // example of usage: > for i in 1..<10..+2 { > print(i) > } > ``` > > The point is, an extension on Range or a custom operator can be constrained > so that Element conforms to Strideable. By contrast, for loops are used to > traverse much more than just ranges. How do you envision your proposed > keyword to interact with everything else? What if I had `var s: Set<Int>` and > tried to write `for i in s by 2`--what do you think should happen? If you > wish to limit `by` to ranges only, what is it about this particular function > that makes it appropriate for a keyword of its own? You will need more to > demonstrate how your proposal achieves what you claim, which is "improved > ease and clearness." > > >> On Thu, Mar 24, 2016 at 2:34 AM, David Knothe via swift-evolution >> <[email protected]> wrote: >> I see your point that a simple extension on ranges and similar types would >> also do the thing. >> >> But I do think that this is not as easy as many users would expect. >> For example, I try to avoid parentheses in for loops as much as I can, but >> in your example, 4 additional brackets are required. A simple keyword >> („step" or „by“) would solve the problem seamlessly. >> >> Also, a new template could be introduced that shows the existence of the new >> keyword: >> <PastedGraphic-1.png> >> >> I think adding such a new keyword would improve the ease and the clearness >> of the Swift language more than just adding some protocol extensions. >> >>> Am 24.03.2016 um 00:02 schrieb Haravikk <[email protected]>: >>> >>> I’m behind the idea in principle, but I’m not so sure we need a new keyword >>> for this: >>> >>> for eachIndex in (1 ..< 10).by(2) { … } >>> >>> The above would be adequate I thin, and just requires a new method on >>> ranges and similar types. This is being discussed, among other things, in >>> the c-style for loop discussion, as these were recently removed but without >>> a replacement for this type of use-case. >>> >>>> On 23 Mar 2016, at 21:40, David Knothe via swift-evolution >>>> <[email protected]> wrote: >>>> >>>> Hi, >>>> >>>> I am new to Swift mailing list so I don’t know if this topic has already >>>> been discussed. >>>> >>>> I like Swift's range operators ..< and … >>>> What I am missing though is an easy operator to create a range with a >>>> specified step. Currently, if you would want to iterate over some odd >>>> numbers, you had to write: >>>> for i in 1.stride(through: 7, by: 2) { … } >>>> What I think would be simpler and more convenient would be something like >>>> the following: >>>> for i in 1 … 7 step 2 { … } . Another option would be for i in 1 … 7; 2 { >>>> … } >>>> The keyword ‚step‘ in this context clearly corresponds to the step of the >>>> range to create or to iterate over. >>>> >>>> Essentially this is a syntactic sugar that makes it easy to create ranges >>>> with a step ≠ 1. What do you think? >>>> _______________________________________________ >>>> swift-evolution mailing list >>>> [email protected] >>>> https://lists.swift.org/mailman/listinfo/swift-evolution >>> >> >> >> _______________________________________________ >> swift-evolution mailing list >> [email protected] >> https://lists.swift.org/mailman/listinfo/swift-evolution >> >
_______________________________________________ swift-evolution mailing list [email protected] https://lists.swift.org/mailman/listinfo/swift-evolution
