<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="">I went back to look at this some more today, and figured it out how to do it (at least in an Xcode 7.3 beta (7D111g) playground) with only one set of argument labels:<div class=""><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(39, 139, 210);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class=""> </span>MutableCollectionType<span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class=""> {</span></div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">subscript</span>(ifExists idx: <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Index</span>) -> <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Generator</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Element</span>? {</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">get</span> {<span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">indices</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">contains</span>(idx) ? <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">self</span>[idx] : <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">nil</span>}</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">set</span> {</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">if</span> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">indices</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">contains</span>(idx) {</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">if</span> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">let</span> nv = newValue {</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">self</span>[idx] = nv</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> } <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">else</span> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">if</span> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">let</span> nilType = <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">self</span>[idx] <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">as</span>? <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">NilLiteralConvertible</span> {</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">self</span>[idx] = (nilType.<span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">dynamicType</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">init</span>(nilLiteral: ()) <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">as</span>! <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Generator</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Element</span>)</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> }</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> }</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> }</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> }</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class="">}</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(39, 139, 210);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class=""> </span>CollectionType<span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class=""> {</span></div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">subscript</span>(ifExists idx: <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Index</span>) -> <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Generator</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Element</span>? {</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">indices</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">contains</span>(idx) ? <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">self</span>[idx] : <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">nil</span></div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> }</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class="">}</div><div class=""><br class=""></div><div class="">And then to test it:</div><div class=""><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(39, 139, 210);" class=""><div style="margin: 0px; line-height: normal;" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">struct</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class=""> Foo : </span>NilLiteralConvertible<span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">, </span>CustomStringConvertible<span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">, </span>IntegerLiteralConvertible<span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class=""> {</span></div><div style="margin: 0px; line-height: normal; color: rgb(147, 161, 161);" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">var</span> value: <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Int</span></div><div style="margin: 0px; line-height: normal; color: rgb(147, 161, 161);" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">init</span>(nilLiteral: ()) {<span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">value</span> = <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Int</span>()}</div><div style="margin: 0px; line-height: normal; color: rgb(147, 161, 161);" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">init</span>(integerLiteral value: <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">IntegerLiteralType</span>) { <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">value</span> = value }</div><div style="margin: 0px; line-height: normal; color: rgb(147, 161, 161);" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">var</span> description: <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">String</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #dc322f" class="">"</span>\<span style="font-variant-ligatures: no-common-ligatures; color: #dc322f" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">value</span><span style="font-variant-ligatures: no-common-ligatures; color: #dc322f" class="">)"</span> }</div><div style="margin: 0px; line-height: normal; color: rgb(147, 161, 161);" class="">}</div><div style="margin: 0px; line-height: normal; color: rgb(147, 161, 161);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">var</span> foo: [<span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Int</span>] = [<span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">0</span>,<span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">1</span>,<span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">2</span>,<span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">3</span>]</div><div style="margin: 0px; line-height: normal; color: rgb(203, 75, 22);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">foo[</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">ifExists: </span><span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">3</span><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">]</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class=""> = </span><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">nil</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class=""> </span>// This *should* be a compiler error, but it isn't because Swift doesn't support getters returning T? and setters takeing T (perhaps there ought to be a proposal about this). In the meantime, this doesn't do anything because you can't assign a nil to an Int.</div><div style="margin: 0px; line-height: normal; color: rgb(203, 75, 22);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">foo</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">) </span>// prints [0, 1, 2, 3]</div><div style="margin: 0px; line-height: normal; color: rgb(147, 161, 161); min-height: 14px;" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">foo[</span>ifExists: <span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">3</span><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">]</span> = <span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">4</span></div><div style="margin: 0px; line-height: normal; color: rgb(203, 75, 22);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">foo</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">) </span>// prints [0, 1, 2, 4]</div><div class=""><br class=""></div></div><div style="margin: 0px; line-height: normal; color: rgb(147, 161, 161);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">var</span> bar: [<span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Int</span>?] = [<span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">0</span>,<span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">1</span>,<span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">2</span>,<span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">3</span>]</div><div style="margin: 0px; line-height: normal; color: rgb(203, 75, 22);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">bar[</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">ifExists: </span><span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">3</span><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">]</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class=""> = </span><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">nil</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">as</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">?? </span>// Again, this *should* be a compiler error… but isn’t (for the same reason as before)</div><div style="margin: 0px; line-height: normal; color: rgb(203, 75, 22);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">bar</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">) </span>// prints [Optional(0), Optional(1), Optional(2), nil]</div><div style="margin: 0px; line-height: normal; color: rgb(203, 75, 22);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">bar[</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">ifExists: </span><span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">2</span><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">]</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class=""> = </span><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">nil</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class=""> </span>// Here, the compiler (eroneously, IMHO) treats this as Optional<Optional<Int>>.None, rather than as Optional<Optional<Int>>.Some(Optional<Int>.None), thus leading to all the trickeries needed to get the nil assignment to go through</div><div style="margin: 0px; line-height: normal; color: rgb(203, 75, 22);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">bar</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">) </span>// prints [Optional(0), Optional(1), nil, nil]</div><div style="margin: 0px; line-height: normal; color: rgb(203, 75, 22);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">bar[</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">ifExists: </span><span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">1</span><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">]</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class=""> = </span><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">nil</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">as</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">? </span>// Wouldn’t ever be an error, since Element == Int?</div><div style="margin: 0px; line-height: normal; color: rgb(203, 75, 22);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">bar</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">) </span>// prints [Optional(0), nil, nil, nil]</div><div style="margin: 0px; line-height: normal; color: rgb(147, 161, 161); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal; color: rgb(147, 161, 161);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">var</span> bof: [<span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">Foo</span>] = [<span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">0</span>,<span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">1</span>,<span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">2</span>,<span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">3</span>]</div><div style="margin: 0px; line-height: normal; color: rgb(203, 75, 22);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">bof[</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">ifExists: </span><span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">3</span><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">]</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class=""> = </span><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">nil</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class=""> </span>// Sets bof[3] to nil, which results in bof[3] = Foo(nilLiteral: ())</div><div style="margin: 0px; line-height: normal; color: rgb(203, 75, 22);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">bof</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">) </span>// prints [0, 1, 2, 0]</div><div class=""><br class=""></div></div></div></div><div class="">(FWIW, I think “failableIndex” or “failableLookup” is a little clearer than “ifExists”, but it’s not keeping me up at night or anything.)</div><div class=""><br class=""></div><div class="">Anyway, does this address everyone’s concerns, at least within what the language allows?</div><div class=""><br class=""></div><div class="">- Dave Sweeris</div><div class=""><br class=""></div><div class=""><div><blockquote type="cite" class=""><div class="">On Feb 1, 2016, at 15:10, 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="">Ok, if the subscript label on the second one isn’t the same as the first one, it works. I’m still not sure why what I wrote earlier today was ambiguous, but this seems to work:<div class=""><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(211, 54, 130);" class="">extension<span style="color: rgb(147, 161, 161);" class=""> </span><span style="color: rgb(39, 139, 210);" class="">Array</span><span style="color: rgb(147, 161, 161);" class=""> {</span></div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> <span style="color: rgb(211, 54, 130);" class="">subscript</span>(failableLookup idx: <span style="color: rgb(39, 139, 210);" class="">Index</span>) -> <span style="color: rgb(39, 139, 210);" class="">Element</span>? {</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> <span style="color: rgb(211, 54, 130);" class="">get</span> { <span style="color: rgb(211, 54, 130);" class="">return</span> (<span style="color: rgb(39, 139, 210);" class="">startIndex</span> ..< <span style="color: rgb(39, 139, 210);" class="">endIndex</span>) ~= idx ? <span style="color: rgb(211, 54, 130);" class="">self</span>[idx] : <span style="color: rgb(211, 54, 130);" class="">nil</span> }</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> <span style="color: rgb(211, 54, 130);" class="">set</span> { <span style="color: rgb(211, 54, 130);" class="">if</span> (<span style="color: rgb(39, 139, 210);" class="">startIndex</span> ..< <span style="color: rgb(39, 139, 210);" class="">endIndex</span>) ~= idx && newValue != <span style="color: rgb(211, 54, 130);" class="">nil</span> { <span style="color: rgb(211, 54, 130);" class="">self</span>[idx] = newValue! } }</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""> }</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class="">}</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="color: rgb(211, 54, 130);" class="">extension</span> <span style="color: rgb(39, 139, 210);" class="">Array</span> <span style="color: rgb(211, 54, 130);" class="">where</span> Element: NilLiteralConvertible {</div><div style="margin: 0px; line-height: normal;" class=""> <span style="color: rgb(211, 54, 130);" class="">subscript</span>(nilConvertible idx: <span style="color: rgb(39, 139, 210);" class="">Index</span>) -> <span style="color: rgb(39, 139, 210);" class="">Element</span>? {</div><div style="margin: 0px; line-height: normal;" class=""> <span style="color: rgb(211, 54, 130);" class="">get</span> { <span style="color: rgb(211, 54, 130);" class="">return</span> (<span style="color: rgb(39, 139, 210);" class="">startIndex</span> ..< <span style="color: rgb(39, 139, 210);" class="">endIndex</span>) ~= idx ? <span style="color: rgb(211, 54, 130);" class="">self</span>[idx] : <span style="color: rgb(211, 54, 130);" class="">nil</span> }</div><div style="margin: 0px; line-height: normal;" class=""> <span style="color: rgb(211, 54, 130);" class="">set</span> { <span style="color: rgb(211, 54, 130);" class="">if</span> (<span style="color: rgb(39, 139, 210);" class="">startIndex</span> ..< <span style="color: rgb(39, 139, 210);" class="">endIndex</span>) ~= idx { <span style="color: rgb(211, 54, 130);" class="">self</span>[idx] = newValue ?? Element(nilLiteral: ())} }</div><div style="margin: 0px; line-height: normal;" class=""> }</div><div style="margin: 0px; line-height: normal;" class="">}</div></div></div><div class=""><br class=""></div><div class="">Seems kinda “hacky”, though, to need the 2nd set argument labels.</div><div class=""><br class=""></div><div class="">Anyway, I hope this helps.</div><div class=""><br class=""></div><div class="">- Dave Sweeris</div>
<br class=""><div class=""><blockquote type="cite" class=""><div class="">On Feb 1, 2016, at 00:53, Rudolf Adamkovič 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=""><div class="">Hi Maximilian,</div><div class=""><br class=""></div><div class="">ah, I see. This is a show stopper then!</div><div class=""><br class=""></div><div class="">From what I imagine, this should not type-check:</div><div class=""><br class=""></div><div class="">var array = [1]</div><div class="">array[ifExists: 0] = nil</div><div class=""><br class=""></div><div class="">… and this should set array[0] to nil:</div><div class=""><br class=""></div><div class=""><div dir="auto" class=""><div class=""><div class=""><div dir="auto" class=""><div class=""><div class=""><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><div class=""><div class=""><div class="" style="margin: 0px; line-height: normal;"><div class="" style="margin: 0px; line-height: normal;">var array: [Int?] = [1]</div><div class="" style="margin: 0px; line-height: normal;">array[ifExists: 0] = nil</div><div class="" style="margin: 0px; line-height: normal;"><br class=""></div><div class="" style="margin: 0px; line-height: normal;">Is it not possible to implement such setter in Swift?</div><div class="" style="margin: 0px; line-height: normal;"><br class=""></div><div class="" style="margin: 0px; line-height: normal;">R+</div></div></div></div></div></div></div></div></div></div></div></div></div></div><br class=""><div class=""><blockquote type="cite" class=""><div class="">On 1 Feb 2016, at 00:07, 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="">The setter of the subscript should be:</div><div class=""><div class=""><div dir="auto" class=""><div class=""><div class=""><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><div class=""><blockquote class="" style="margin: 0px 0px 0px 40px; border: none; padding: 0px;"><div class=""><div class="" style="margin: 0px; line-height: normal;"><div class="" style="margin: 0px; line-height: normal;"><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><span class=""><br class=""></span></span></font></div><div class="" style="margin: 0px; line-height: normal;"><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><span class="">set</span> {</span></font></div><div class="" style="margin: 0px; line-height: normal;"><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""> <span class="">if</span> <span class="">self</span>.indices ~= index && newValue != <span class="">nil</span> {</span></font></div><div class="" style="margin: 0px; line-height: normal;"><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""> <span class="">self</span>[index] = newValue!</span></font></div><div class="" style="margin: 0px; line-height: normal;"><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""> }</span></font></div><div class="" style="margin: 0px; line-height: normal;"><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">}</span></font></div><div class="" style="margin: 0px; line-height: normal;"><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></font></div><div class="" style="margin: 0px; line-height: normal;"><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">Since "newValue" is of type "Element?".</span></font></div><div class="" style="margin: 0px; line-height: normal;"><br class=""></div><div class="" style="margin: 0px; line-height: normal;">It seems that this subscript could also be added to "CollectionType" and "MutableCollectionType".</div><div class="" style="margin: 0px; line-height: normal;"><br class=""></div><div class="" style="margin: 0px; line-height: normal;">The setter is weird because you can use an optional element:</div><div class="" style="margin: 0px; line-height: normal;"><br class=""></div><div class="" style="margin: 0px; line-height: normal;">var arr = [1]</div><div class="" style="margin: 0px; line-height: normal;">// is valid but doesn't set the first element</div><div class="" style="margin: 0px; line-height: normal;">arr[ifExists: 0] = nil</div><div class="" style="margin: 0px; line-height: normal;"><br class=""></div><div class="" style="margin: 0px; line-height: normal;">var arr2: [Int?] = [1]</div><div class="" style="margin: 0px; line-height: normal;">arr2[ifExists: 0] = nil // changes nothing</div><div class="" style="margin: 0px; line-height: normal;">arr2[ifExists: 0] = .Some(nil) // sets first element to nil : arr2 == [nil]</div><div class="" style="margin: 0px; line-height: normal;"><br class=""></div><div class="" style="margin: 0px; line-height: normal;">I don't know whether a setter should be added at all.</div><div class="" style="margin: 0px; line-height: normal;"><br class=""></div><div class="" style="margin: 0px; line-height: normal;">- Maximilian</div></div></div></blockquote></div></div></div></div></div></div></div></div></div><div class=""><br class="">Am 31.01.2016 um 23:38 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="">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></div></blockquote></div></div></blockquote></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=""><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>_______________________________________________<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></div></body></html>