<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 20 Jan 2017, at 07:41, Russ Bishop via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Jan 19, 2017, at 4:17 PM, Xiaodi Wu via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><div class=""><div dir="ltr" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><div class="gmail_extra"><div class="gmail_quote"><div class=""><br class=""></div><div class="">For those times when you _don't_ know how many elements there are, don't care, and for some reason can't be bothered to get `array.count`, but you need to explicitly access an element by its index *and* have a useful fallback value, IMO it's reasonable to have an alternative subscript like the proposed `array[lenient: 10]`. But with facilities like `for...in`, `map`, etc., and others like `count` and `enumerated`, it's hard to argue that it's nearly as common a scenario as those where you are given a known-good index.</div><div class=""><br class=""></div></div></div></div></div></blockquote><br class=""></div><div class="">I’m not sure why people keep asking for this; the extension is trivial so anyone who wants it can have it:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">extension&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures;" class=""><span style="color: rgb(112, 61, 170);" class="">Collection</span></span><span style="font-variant-ligatures: no-common-ligatures;" class=""><br class="">{</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">subscript</span><span style="font-variant-ligatures: no-common-ligatures" class="">(ifExists index: Index) -&gt; Iterator.Element? {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">guard</span><span style="font-variant-ligatures: no-common-ligatures" class=""> index </span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">&lt;</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">endIndex</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">else</span><span style="font-variant-ligatures: no-common-ligatures" class=""> { </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">nil</span><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class="">[index]</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div></div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">// won't assert!</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">myArray[ifExists: </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">42</span><span style="font-variant-ligatures: no-common-ligatures" class="">]</span></div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Russ</div><br class=""></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""><div class="">Amen!</div><div class=""><br class=""></div><div class="">Although technically, you should do (startIndex..&lt;endIndex).contains(index) — what about if somebody asked for something lower than startIndex? But yeah, it’s such a trivial extension.</div><div class=""><br class=""></div><div class="">Asking a Collection for an item at a specific index which does not exist is, let’s be clear, a nonsense operation.</div><div class=""><br class=""></div><div class="">let myArray = [] // empty array</div><div class="">// would all be valid</div><div class="">myArray[0]</div><div class="">myArray[-1]</div><div class="">myArray[Int64.max]</div><div class=""><br class=""></div><div class="">I think that’s ludicrous. It completely flies in the face of our current indexing/collection model, which is this: items are retrieved by an associated Index type, and the various types of collection define how you are able to actually get one of those indexes:</div><div class=""><br class=""></div><div class="">- Collection: Provides startIndex/endIndex anchors, can only get the Index after an existing Index</div><div class="">- BidirectionalCollection: Can also get the Index before an existing Index</div><div class="">- RandomAccessCollection: Can also get the Index at an arbitrary offset from an existing Index</div><div class=""><br class=""></div><div class="">So essentially what is being proposed is that you can subscript Collections by invalid Indexes (maybe an Index from another instance of that Collection, or one which in other ways violates the axioms of the indexing model). Not only that, but everything which uses an Index (e.g. suffix(from:)) would have to also return an Optional to account for a nonsense index being given.</div><div class=""><br class=""></div><div class="">Also, let’s not beat around the bush: this isn’t a general Collection feature, this is only for Array (because its Index is not an opaque type — if it was, invalid indexes would be even more obviously a result of your own bad logic).</div><div class=""><br class=""></div><div class="">So basically this is being driven by programmers who are **abusing** Array and its indexes. If Array.Index was an opaque type, none of this would be possible. For your convenience, the index was made an integer, but that doesn’t mean you can just pass in *any* integer. You still need to abide by the general Collection indexing model.</div><div class=""><br class=""></div><div class="">- Karl</div></body></html>