<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div></div><div><br></div><div>Am 03.06.2016 um 21:20 schrieb Haravikk &lt;<a href="mailto:swift-evolution@haravikk.me">swift-evolution@haravikk.me</a>&gt;:<br></div><blockquote type="cite"><div><br class=""><div><blockquote type="cite" class=""><div class="">On 1 Jun 2016, at 05:51, Thorsten Seitz &lt;<a href="mailto:tseitz42@icloud.com" class="">tseitz42@icloud.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="content-type" content="text/html; charset=utf-8" class=""><div dir="auto" class=""><div class=""></div><div class="">I like this idea. The problem is that it would require that we have an Index.NonNegativeDistance as argument to really make it statically safe. And we would have to have methods producing these, probably as optional return values.</div><div class="">Otherwise we won't have achieved statically safety but effectively just better documentation about the capabilities of the respective collection.</div><div class=""><br class=""></div><div class="">-Thorsten&nbsp;</div></div></div></blockquote><div><br class=""></div><div>So you’d see something like:</div><div><br class=""></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>public func distance(from start:Index, advancedTo end:Index) -&gt; Index.NonNegativeDistance? { … }</font></div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>public func distance(from start:Index, reversedTo end:Index) -&gt; Index.NonNegativeDistance? { … }</font></div><div><br class=""><div>Or something to that effect anyway, with the single-direction index (and formIndex etc.) methods taking the same type?. That’s clever, eliminates the run-time error for distance in many cases with a type-check at compile time instead, neat!</div></div></div></blockquote><div><br></div>Yes, that's what I meant.<div><br><blockquote type="cite"><div><div><div><br class=""></div><div>This could actually present an interesting possibility by designing around the fixed direction methods first, e.g- the NonNegativeDistance could be a Uint, and regular Distance would be an enum with .Forward(UInt) and .Backward(UInt) cases. I’m </div></div></div></blockquote><div><br></div>That's a good idea! I'd like to see how some real algorithms would look like with that approach.</div><div><br></div><div>-Thorsten&nbsp;</div><div><br><blockquote type="cite"><div><div><div>currently porting some of my simpler collections to Swift 3 to get some more experience with the new indexing model and other changes, and this would be a nice way to do it for some of these, rather than relying on a regular Int.</div><br class=""><blockquote type="cite" class=""><div class=""><div dir="auto" class=""><div class="">Am 31.05.2016 um 14:46 schrieb Haravikk via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt;:<br class=""><br class=""></div><blockquote type="cite" class=""><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class="">So for Swift 3 we’re going to have the great new indexing model that performs index manipulation through the collection to which an index belongs.<div class=""><br class=""></div><div class="">However, it retains one of the things I didn’t like about the old model, which is that the distinction between forward/backward only types is a bit fuzzy, since the single advancedBy() method, now the index(:offsetBy:) method, was used for both forward and backward movement, which seems contradictory compared to the forward/backward only single-step methods.</div><div class=""><br class=""></div><div class="">Anyway, I’m wondering what people’s thoughts would be on tweaking the formula slightly such that there are methods that only work in a particular direction, i.e- we’d have three main variations of the methods like so:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">public</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> index(</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">_</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> index:</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Index</span><span style="font-variant-ligatures: no-common-ligatures;" class="">, advancedBy:</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Index</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Distance</span><span style="font-variant-ligatures: no-common-ligatures;" class="">) -&gt; </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Index</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> { … } </span>// Available on forward and bidirectional collections</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">public</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> index(</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">_</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> index:</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Index</span><span style="font-variant-ligatures: no-common-ligatures;" class="">, reversedBy:</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Index</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Distance</span><span style="font-variant-ligatures: no-common-ligatures;" class="">) -&gt; </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Index</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> { … } </span>// Available on reverse and bidirectional collections</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">public</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> index(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">_</span> index:<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Index</span>, offsetBy:<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Index</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Distance</span>) -&gt; <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Index</span> { … } <span style="font-variant-ligatures: no-common-ligatures; color: #008400" class="">// Available only on bidirectional collections</span></div></div><div class=""><br class=""></div><div class="">(note, the naming isn’t definite, as reversed may not be clear enough, it’s just an example for now)</div><div class=""><br class=""></div><div class="">There are three reasons I’d prefer this:</div><div class=""><br class=""></div><div class="">The first is that I can pass the same distance into either of the first two methods, and any negation etc. is handled internally. In essence I shouldn’t have to handle negative distances at all when working with the first two methods. So if I’m working with a step size of 5, I can just pass that into the appropriate method, I never have to do anything with it the value itself.</div><div class=""><br class=""></div><div class="">The second benefit is that there should be no uncertainty about the capabilities of the type you’re using; if it doesn’t have the index(:reversedBy:) method then you can’t go backwards, same as index(before:) and index(after:).</div><div class=""><br class=""></div><div class="">The third and main benefit is that the methods are just more explicit about what they do, and what direction you can go in; passing negatives into either of the first two would produce errors outright, allowing you to pick on mistakes in these cases.</div><div class=""><br class=""></div><div class="">The other main thing is that offsetBy doesn’t indicate whether a type supports forward-only offsets, you have to read the documentation to determine this either in the method itself or the type, whereas the presence or absence of the first two variants are pretty clear.</div><div class=""><br class=""></div><div class="">Currently the offsetBy, and the previous advancedBy(), methods require forward-only types to produce fatal errors if handed a negative distance, and vice versa for backward-only types, which can only produce errors at runtime, whereas the presence or absence of the first two methods can be handled during development. You could still pass a negative value and end up with a runtime error instead of course, but for the types of common uses they’re intended for you should be unlikely to produce one.</div><div class=""><br class=""></div><div class="">The offsetBy form would still exist for bidirectional collections, but would only really be used when you need to do more complex index/distance manipulation outside of the type where a calculation might produce either positive or negative values (e.g- if you're calculating the distance and don’t know where two indices are in relation to each other), the rest of the time you should try to use the more specific, single-direction forms as they clarify your intent and can help to catch mistakes if you’ve incorrectly generated a distance for example.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Just curious what other people’s thoughts are about this?</div><div class=""><br class=""></div><div class="">I intended to mention this a lot sooner (to change advancedBy), but then I find out about the new indexing model so thought I’d wait until afterwards, then completely forgot =)</div></div></blockquote><blockquote type="cite" class=""><div class=""><span class="">_______________________________________________</span><br class=""><span class="">swift-evolution mailing list</span><br class=""><span class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a></span><br class=""><span class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br class=""></div></blockquote></div></div></blockquote></div><br class=""></div></blockquote></div></body></html>