<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=""><div class="">All right, I put together a proposal:</div><div class=""><br class=""></div><div class=""><a href="https://github.com/salutis/swift-evolution/blob/master/proposals/XXXX-runtime-safe-array-subscripting.md" class="">https://github.com/salutis/swift-evolution/blob/master/proposals/XXXX-runtime-safe-array-subscripting.md</a></div><div class=""><br class=""></div><div class="">… and opened a PR:</div><div class=""><br class=""></div><div class=""><a href="https://github.com/apple/swift-evolution/pull/133" class="">https://github.com/apple/swift-evolution/pull/133</a></div><div class=""><br class=""></div><div class="">Let’s see how this goes.</div><div class=""><br class=""></div><div class="">R+</div><br class=""><div><blockquote type="cite" class=""><div class="">On 19 Jan 2016, at 06:53, Thorsten Seitz <<a href="mailto:tseitz42@icloud.com" class="">tseitz42@icloud.com</a>> 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="">Good points! </div><div class="">I like ifExists: even more than failable:</div><div class=""><br class=""></div><div class="">-Thorsten </div><div class=""><br class="">Am 17.01.2016 um 11:26 schrieb Rudolf Adamkovič via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>>:<br class=""><br class=""></div><blockquote type="cite" class=""><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div class="">Dave, that’s a great point. The word “safe” doesn’t really work for setters, you’re totally right.</div><div class=""><br class=""></div><div class="">What about this:</div><div class=""><br class=""></div><div class=""><pre class="" style="box-sizing: border-box; overflow: auto; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; padding: 16px; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-wrap: normal; word-break: normal;"><font face="Menlo" class=""><span class="" style="font-size: 11px;"><span class="pl-k" style="color: rgb(167, 29, 93); box-sizing: border-box;">let</span><font color="#333333" class=""> array </font><span class="pl-k" style="color: rgb(167, 29, 93); box-sizing: border-box;">=</span><font color="#333333" class=""> [</font><font color="#0086b3" class="">0</font><font color="#333333" class="">, </font><font color="#0086b3" class="">1</font><font color="#333333" class="">, </font><font color="#0086b3" class="">2</font><font color="#333333" class="">]
array[ifExists: </font><span class="pl-c1" style="color: rgb(0, 134, 179); box-sizing: border-box;">0</span><font color="#333333" class="">] </font><span class="pl-c" style="color: rgb(150, 152, 150); box-sizing: border-box;">// same as array[0]</span><font color="#333333" class="">
array[ifExists: </font><span class="pl-c1" style="color: rgb(0, 134, 179); box-sizing: border-box;">3</span><font color="#333333" class="">] </font><span class="pl-c" style="color: rgb(150, 152, 150); box-sizing: border-box;">// doesn’t exist, evaluates to nil</span></span></font></pre><div class=""><br class=""></div></div><div class=""><pre class="" style="box-sizing: border-box; overflow: auto; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; padding: 16px; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-wrap: normal; word-break: normal;"><font face="Menlo" class=""><span class="" style="font-size: 11px;"><font color="#a71d5d" class="">var</font><font color="#333333" class=""> array </font><span class="pl-k" style="color: rgb(167, 29, 93); box-sizing: border-box;">=</span><font color="#333333" class=""> [</font><font color="#0086b3" class="">0</font><font color="#333333" class="">,</font><font color="#0086b3" class=""> 1</font><font color="#333333" class="">, </font><font color="#0086b3" class="">2</font><font color="#333333" class="">]
array[ifExists: </font><font color="#0086b3" class="">0</font><font color="#333333" class="">] = 42 </font><span class="pl-c" style="color: rgb(150, 152, 150); box-sizing: border-box;">// same as array[0] = 42</span><font color="#333333" class="">
array[ifExists: </font><font color="#0086b3" class="">3</font><font color="#333333" class="">] = 42 </font><span class="pl-c" style="color: rgb(150, 152, 150); box-sizing: border-box;">// doesn’t exist, does nothing</span><br class=""></span></font></pre><div class=""><br class=""></div></div><div class="">Reads a bit better than “failable" to me.</div><div class=""><br class=""></div><div class="">Ideas?</div><div class=""><br class=""></div><div class="">R+</div><br class=""><div class=""><blockquote type="cite" class=""><div class="">On 17 Jan 2016, at 04:06, Dave via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> 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="">So, this?<blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">subscript</span>(safe index: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Index</span>) -> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Element</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; color: #bb2ca2" class="">get</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.indices ~= index ? <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>[index] : <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">nil</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; color: #bb2ca2" class="">set</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; color: #bb2ca2" class="">if</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.indices ~= index {</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; color: #bb2ca2" class="">self</span>[index] = newValue!</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div></div></blockquote><div class=""><div class=""><br class=""></div><div class="">The problem with that is again semantics. You’re (sorta) saying “here, let me safely store this for you”, and then it doesn’t get stored because “safe” applies to the indexing as opposed to the assignment. What about “failable”?</div><div class=""><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px;" class=""><div class=""><div style="margin: 0px; line-height: normal;" class=""><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">subscript</span>(failable index: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Index</span>) -> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Element</span>? {</div><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">get</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.indices ~= index ? <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>[index] : <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">nil</span> }</div><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">set</span> {</div><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.indices ~= index && newValue != <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">nil</span> {</div><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>[index] = newValue!</div><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal;" class=""> }</div><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal;" class=""> }</div><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal;" class="">}</div><div style="margin: 0px; line-height: normal;" class=""><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class=""><br class=""></span></div><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> foo = [<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span>] <span style="color: rgb(0, 132, 0);" class="">// normal</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-family: Menlo; font-size: 11px; color: rgb(187, 44, 162);" class="">var</span><font face="Menlo" class=""><span style="font-size: 11px;" class=""> bar = </span></font><span style="font-family: Menlo; font-size: 11px; color: rgb(79, 129, 135);" class="">foo</span><span style="font-family: Menlo; font-size: 11px; color: rgb(49, 89, 93);" class="">[</span><font face="Menlo" class=""><span style="font-size: 11px;" class="">failable: </span></font><span style="font-family: Menlo; font-size: 11px; color: rgb(39, 42, 216);" class="">2</span><span style="font-family: Menlo; font-size: 11px; color: rgb(49, 89, 93);" class="">] //nil, but it</span><font color="#31595d" face="Menlo" class=""><span style="font-size: 11px;" class="">’s clearly failable, so no surprise </span></font></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-family: Menlo; font-size: 11px; color: rgb(79, 129, 135);" class="">foo</span><span style="font-family: Menlo; font-size: 11px; color: rgb(49, 89, 93);" class="">[</span><font face="Menlo" class=""><span style="font-size: 11px;" class="">failable: </span></font><span style="font-family: Menlo; font-size: 11px; color: rgb(39, 42, 216);" class="">2</span><span style="font-family: Menlo; font-size: 11px; color: rgb(49, 89, 93);" class="">]</span><font face="Menlo" class=""><span style="font-size: 11px;" class=""> = </span></font><span style="font-family: Menlo; font-size: 11px; color: rgb(39, 42, 216);" class="">3</span><span style="font-family: Menlo; font-size: 11px; color: rgb(49, 89, 93);" class=""> //nothing happens, but again it</span><font color="#31595d" face="Menlo" style="font-family: Menlo; font-size: 11px;" class="">’s clearly failable, so we know the assignment might not</font><font color="#31595d" face="Menlo" class=""><span style="font-size: 11px;" class="">’ve stuck</span></font></div><div style="font-family: Menlo; font-size: 11px;" class=""><br class=""></div></div></div></div></blockquote></div><br class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Jan 16, 2016, at 17:55, Maximilian Hünenberger <<a href="mailto:m.huenenberger@me.com" class="">m.huenenberger@me.com</a>> 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="">It is true that the setter could cause some confusion in the first place but I think of these particular setters as optional chaining:</div><div class=""><br class=""></div><div class="">person.residence?.address = "1 Infinite Loop"</div><div class=""><br class=""></div><div class="">Where the `address` is not set when `residence` is nil. So it’s like the array is nil/not defined if you access it out of bounds.</div></div></div></blockquote><div class=""><br class=""></div>I really like that! I think the optional chaining syntax would make the semantics crystal clear</div></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><div class=""><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal;" class=""><span style="color: rgb(0, 132, 0);" class="">// note the ? after subscript</span></div><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal;" class=""><span style="color: rgb(187, 44, 162);" class="">subscript</span>?(index: <span style="color: rgb(112, 61, 170);" class="">Index</span>) -> <span style="color: rgb(112, 61, 170);" class="">Element</span>? {…}</div><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> foo = [<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span>] <span style="color: rgb(0, 132, 0);" class="">// normal</span></div></div></div><div class=""><div class=""><div style="margin: 0px; line-height: normal;" class=""><span style="font-family: Menlo; font-size: 11px; color: rgb(187, 44, 162);" class="">var</span><font face="Menlo" class=""><span style="font-size: 11px;" class=""> bar = </span></font><span style="font-family: Menlo; font-size: 11px; color: rgb(79, 129, 135);" class="">foo</span><font face="Menlo" class=""><span style="font-size: 11px;" class="">?[</span></font><span style="font-family: Menlo; font-size: 11px; color: rgb(39, 42, 216);" class="">2</span><font face="Menlo" class=""><span style="font-size: 11px;" class="">] </span></font><font color="#008400" face="Menlo" class=""><span style="font-size: 11px;" class="">// returns an Int?, just like with [Int]?</span></font></div></div></div><div class=""><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">foo</span>?[<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">2</span>] = <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">3 </span><span style="color: rgb(0, 132, 0);" class="">// performs assignment if possible</span><span style="color: rgb(0, 132, 0);" class="">, just like with [Int]?</span></div></div></div></blockquote><div class=""><div class=""><div class=""><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class=""><br class=""></span></div></div>I don’t know what it would take to get the “?” to apply to `[]` instead of `foo`. I have no (well, little) doubt that it’s <i class="">possible</i>, but I don’t know how hard it’d be or if it’d break anything. Off the top of my head, even though the results are just like if foo were `[Int]?`, the compiled code would <i class="">not</i> be the same. If we’re only talking about “safely” (or whatever the adverb is) indexing normal arrays, it’s not an issue because there’s nothing for the “?” to unwrap, so the programmer must be referring to the “safe” variant. With [Int]? though, are we unwrapping it? I guess we’d have to since `Optional<T>` isn’t subscriptable, so… Maybe this?</div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> optionalfoo:[<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span>]? = [<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span>]</div></div><div class=""><div style="margin: 0px; line-height: normal;" class=""><span style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px;" class="">var</span><span style="font-family: Menlo; font-size: 11px;" class=""> bar = </span><font color="#4f8187" face="Menlo" class=""><span style="font-size: 11px;" class="">optionalfoo</span></font><span style="font-family: Menlo; font-size: 11px;" class="">??[</span><span style="color: rgb(39, 42, 216); font-family: Menlo; font-size: 11px;" class="">2</span><span style="font-family: Menlo; font-size: 11px;" class="">] </span><font color="#008400" face="Menlo" class=""><span style="font-size: 11px;" class="">// “??”, so the optional is unwrapped *and* we use the “safe" subscript. bar = nil</span></font></div></div><div class=""><div style="margin: 0px; line-height: normal;" class=""><span style="font-family: Menlo; font-size: 11px; color: rgb(187, 44, 162);" class="">var</span><font face="Menlo" style="font-family: Menlo; font-size: 11px;" class=""><font color="#4f8187" class=""> </font>crashBar<font color="#4f8187" class=""> = </font></font><font color="#4f8187" style="font-family: Menlo; font-size: 11px;" class="">optionalfoo</font><span style="font-family: Menlo; font-size: 11px;" class="">?[</span><span style="font-family: Menlo; font-size: 11px; color: rgb(39, 42, 216);" class="">2</span><span style="font-family: Menlo; font-size: 11px;" class="">]</span><font color="#4f8187" style="font-family: Menlo; font-size: 11px;" class=""> </font><font color="#008400" face="Menlo" class=""><span style="font-size: 11px;" class="">// “?”, so the optional is unwrapped, but we use the normal subscript and crash</span></font></div><div class=""></div></div></blockquote><div class=""><br class=""></div><div class="">I’m not at all certain what the implications of this would be for the rest of the language, though. Hmm…</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><div class="">- Dave Sweeris</div></div><div class=""><br class=""></div></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></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=""></body></html>