<div dir="ltr"><div>is this syntax reasonably simple to implement? Or is there another solution that would work with less impact in terms of design? </div><div>I mean the subscript with a label on it, i.e. <font face="monospace, monospace">collection[label: Range&lt;Index&gt;]</font><div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div style="font-size:small">It&#39;s been a while since the last feedback, so I&#39;m doing some rewriting on this proposal and still considering to submit it for review.</div><div style="font-size:small"><br></div><div style="font-size:small">- Luis</div></div></div></div></div></div></div></div></div></div>
<br><div class="gmail_quote">On Wed, Apr 13, 2016 at 10:29 PM, Dave Abrahams via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span class=""><br>
on Wed Apr 13 2016, Maximilian Hünenberger &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br>
<br>
&gt; Should this new operator form a new range? How can this range know about the<br>
&gt; array&#39;s indices?<br>
&gt;<br>
&gt; A while ago there was a proposal (unfortunately it was not discussed enough)<br>
&gt; which introduced safe array indexing:<br>
&gt;<br>
&gt; array[safe: 3] // returns nil if index out of bounds<br>
<br>
</span>Wrong label, but I wouldn&#39;t be opposed to adding such an operator for<br>
all Collections.<br>
<span class=""><br>
&gt; So another way to handle this issue would be to make another subscript like:<br>
&gt;<br>
&gt; array[truncate: -1...6]<br>
<br>
</span>That approach makes sense too.  But then do we add<br>
<br>
  x[python: 0..&lt;-2] // all but the last two elements?<br>
<br>
;^)<br>
<div class=""><div class="h5"><br>
&gt; Best regards<br>
&gt; - Maximilian<br>
&gt;<br>
&gt; Am 12.04.2016 um 01:21 schrieb Luis Henrique B. Sousa via swift-evolution<br>
&gt; &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt;:<br>
&gt;<br>
&gt;         The idea of having a new operator following the principles of overflow<br>
&gt;     operators looks great. Two distinct operators doing implicit and explicitly<br>
&gt;     might really be a good way to go; it would be concise and wouldn&#39;t look like<br>
&gt;     some magic happened behind the scenes. I&#39;d like to hear more opinions about<br>
&gt;     it.<br>
&gt;<br>
&gt;     &gt; what we&#39;ll have in case a[-1 &amp;..&lt; 5]? should this raise error or become [0<br>
&gt;     ..&lt; 3] ? I think, the latter.<br>
&gt;     I agree here, I&#39;d choose the latter.<br>
&gt;<br>
&gt;     From my perspective, the behaviour I&#39;m proposing is what a considerable<br>
&gt;     number of users expect, especially if coming from other languages that<br>
&gt;     follow that path. Of course I&#39;m not comparing languages here, but<br>
&gt;     considering the Swift principles of being a safer language, in my opinion<br>
&gt;     we&#39;d rather have a partial slice than a crash in execution time (when the<br>
&gt;     user is not totally aware of it).<br>
&gt;<br>
&gt;     Many thanks for all your additions so far. It&#39;s really good to see that<br>
&gt;     these things are not set in stone yet.<br>
&gt;<br>
&gt;     - Luis<br>
&gt;<br>
&gt;     On Apr 11, 2016 4:21 PM, &quot;Vladimir.S via swift-evolution&quot;<br>
&gt;     &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br>
&gt;<br>
&gt;     +1 for the idea &quot;in general&quot;. But I also think that explicit is better than<br>
&gt;         implicit, especially if we deal with possible errors. Just like we work<br>
&gt;         in Swift with integer overflow : &#39;+&#39; will generate run time error, but<br>
&gt;         saying &amp;+ we point Swift that we know what we do.<br>
&gt;<br>
&gt;         but.. what we&#39;ll have in case a[-1 &amp;..&lt; 5]? should this raise error or<br>
&gt;         become [0 ..&lt; 3] ? I think, the latter.<br>
&gt;<br>
&gt;         On 11.04.2016 17:02, Haravikk via swift-evolution wrote:<br>
&gt;<br>
&gt;         I like the idea in theory, but the question is; is it really safer to<br>
&gt;             return a result that the developer may not have wanted, versus an<br>
&gt;             error<br>
&gt;             indicating that a mistake may have been made? I wonder if perhaps<br>
&gt;             there<br>
&gt;             could be an alternative, such as a variation of the operator like<br>
&gt;             so:<br>
&gt;<br>
&gt;             let b = a [0 &amp;..&lt; 5]// Equivalent to let b = a[0 ..&lt; min(5,<br>
&gt;             a.endIndex)],<br>
&gt;             becomes let b = a[0 ..&lt; 3]<br>
&gt;<br>
&gt;             I’m just not sure that we can assume that an array index out of<br>
&gt;             range error<br>
&gt;             is okay without some kind of indication from the developer, as<br>
&gt;             otherwise we<br>
&gt;             could end up returning a partial slice, which could end up causing<br>
&gt;             an error<br>
&gt;             elsewhere where the size of the slice is assumed to be 5 but isn’t.<br>
&gt;<br>
&gt;                         On 11 Apr 2016, at 13:23, Luis Henrique B. Sousa via<br>
&gt;                 swift-evolution<br>
&gt;                 &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
&gt;                 &lt;mailto:<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt;&gt;<br>
&gt;                 wrote:<br>
&gt;<br>
&gt;                 This proposal seeks to provide a safer ..&lt; (aka half-open range<br>
&gt;                 operator)<br>
&gt;                 in order to avoid **Array index out of range** errors in<br>
&gt;                 execution time.<br>
&gt;<br>
&gt;                 Here is my first draft for this proposal:<br>
&gt;                 <a href="https://github.com/luish/swift-evolution/blob/half-open-range-operator/proposals/nnnn-safer-half-open-range-operator.md" rel="noreferrer" target="_blank">https://github.com/luish/swift-evolution/blob/half-open-range-operator/proposals/nnnn-safer-half-open-range-operator.md</a><br>
&gt;<br>
&gt;                 In short, doing that in Swift causes a runtime error:<br>
&gt;<br>
&gt;                 leta =[1,2,3]<br>
&gt;                 letb =a[0..&lt;5]<br>
&gt;                 print(b)<br>
&gt;<br>
&gt;                 &gt; Error running code:<br>
&gt;                 &gt; fatal error: Array index out of range<br>
&gt;<br>
&gt;                 The proposed solution is to slice the array returning all<br>
&gt;                 elements that<br>
&gt;                 are below the half-open operator, even though the number of<br>
&gt;                 elements is<br>
&gt;                 lesser than the ending of the half-open operator. So the example<br>
&gt;                 above<br>
&gt;                 would return [1,2,3].<br>
&gt;                 We can see this very behaviour in other languages, such as<br>
&gt;                 Python and<br>
&gt;                 Ruby as shown in the proposal draft.<br>
&gt;<br>
&gt;                 This would eliminate the need for verifications on the array<br>
&gt;                 size before<br>
&gt;                 slicing it -- and consequently runtime errors in cases when the<br>
&gt;                 programmer didn&#39;t.<br>
&gt;<br>
&gt;                 Viewing that it is my very first proposal, any feedback will be<br>
&gt;                 helpful.<br>
&gt;<br>
&gt;                 Thanks!<br>
&gt;<br>
&gt;                 Luis Henrique Borges<br>
&gt;                 @luishborges<br>
&gt;                 _______________________________________________<br>
&gt;                 swift-evolution mailing list<br>
&gt;                 <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
&gt;                 &lt;mailto:<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt;<br>
&gt;                 <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
&gt;<br>
&gt;             _______________________________________________<br>
&gt;             swift-evolution mailing list<br>
&gt;             <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
&gt;             <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
&gt;<br>
&gt;         _______________________________________________<br>
&gt;         swift-evolution mailing list<br>
&gt;         <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
&gt;         <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
&gt;<br>
&gt;     _______________________________________________<br>
&gt;     swift-evolution mailing list<br>
&gt;     <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
&gt;     <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; swift-evolution mailing list<br>
&gt; <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br>
</div></div><span class=""><font color="#888888">--<br>
Dave<br>
</font></span><div class=""><div class="h5"><br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</div></div></blockquote></div><br></div></div></div></div>