<div dir="ltr">Has it been considered to just do this:<div><br><div>extension CollectionType {<br><div><font face="monospace, monospace"> func at(index: Index) -> Generator.Element? {</font></div><div><span style="font-family:monospace,monospace"> </span><span style="font-family:monospace,monospace"> </span><font face="monospace, monospace"> return self.indices ~= index ? </font><span style="font-family:monospace,monospace">self[index] : nil</span></div><div><span style="font-family:monospace,monospace"> </span><span style="font-family:monospace,monospace"> </span><font face="monospace, monospace">}</font></div></div><div><font face="monospace, monospace"> func update(value: Generator.Element, atIndex: Index) -> </font><span style="font-family:monospace,monospace">Generator.Element? {</span></div><div><span style="font-family:monospace,monospace"> guard </span><span style="font-family:monospace,monospace">self.indices ~= index else { return nil }</span></div><div><span style="font-family:monospace,monospace"> let oldValue = </span><span style="font-family:monospace,monospace">self[index]</span></div><div><span style="font-family:monospace,monospace"> self[index] = value</span></div><div><span style="font-family:monospace,monospace"> return oldValue</span></div><div><span style="font-family:monospace,monospace"> }</span></div><div><font face="monospace, monospace">}</font></div><div><font face="monospace, monospace"><br></font></div>Compare:<div><span style="font-family:monospace,monospace"> </span><font face="monospace, monospace">let x = array[safe: index]</font></div><div><span style="font-family:monospace,monospace"> let y =</span><span style="font-family:monospace,monospace"> </span><font face="monospace, monospace"><a href="http://array.at">array.at</a>(</font><span style="font-family:monospace,monospace">index</span><font face="monospace, monospace">)</font></div><div><font face="monospace, monospace"><br></font></div></div>It's more concise (for the getter), doesn't have to introduce new syntax, works in current swift, and it doesn't have ambiguity about nil in a subscript setter.<div><br></div><div>There's precedent for the update function in Dictionary:</div><font face="monospace, monospace"> public mutating func updateValue(value: Value, forKey key: Key) -> Value?</font><div><div><br></div><div>It would be a shame (and surprising/unsafe) to have to do this:<br><div><font face="monospace, monospace"><br></font></div></div><div><font face="monospace, monospace"> array[safe: index] = .Some(nil) // stores nil</font></div><div><font face="monospace, monospace"> </font><span style="font-family:monospace,monospace">array[safe: index] = nil // deletes a value</span><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"><br></font></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Feb 6, 2016 at 10:58 AM, Maximilian Hünenberger <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div></div><div>You are totally right. The return type is "Int??".</div><div><br></div><div>My point was that if we allowed something like this (as suggested by Dave Sweeris I think):</div><span class=""><div><br></div><div> var array: [Int?] = [1]</div><div> array[ifExists: 0] = nil</div><div><br></div></span><div>To set the element at index 0 to nil instead of doing nothing.</div><div>The next example would also set index 0 to nil even though the getter failed:</div><div><br></div><div> <span style="background-color:rgba(255,255,255,0)">array[ifExists: 0] = array[ifExists: 1]</span></div><div><br></div><div><br></div><div>- Maximilian</div><div><div class="h5"><div><br>Am 05.02.2016 um 10:20 schrieb Haravikk <<a href="mailto:swift-evolution@haravikk.me" target="_blank">swift-evolution@haravikk.me</a>>:<br><br></div><blockquote type="cite"><div><br><div><blockquote type="cite"><div>On 4 Feb 2016, at 20:24, Maximilian Hünenberger via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><br><div><div style="word-wrap:break-word"><div>I just realized that the normal setter for failable lookups is very nice in case of assigning/swapping:</div><div><br></div><div><blockquote type="cite"><div style="word-wrap:break-word"><div><div style="margin:0px;line-height:normal;font-family:'Fira Mono';color:rgb(147,161,161)"><div style="margin:0px;line-height:normal;color:rgb(211,54,130)">extension<span style="color:rgb(147,161,161)"> </span><span style="color:rgb(39,139,210)">Array</span><span style="color:rgb(147,161,161)"> {</span></div><div style="margin:0px;line-height:normal"> <span style="color:rgb(211,54,130)">subscript</span>(ifExists idx: <span style="color:rgb(39,139,210)">Index</span>) -> <span style="color:rgb(39,139,210)">Element</span>? {</div><div style="margin:0px;line-height:normal"> <span style="color:rgb(211,54,130)">get</span> { <span style="color:rgb(211,54,130)">return</span> (<span style="color:rgb(39,139,210)">startIndex</span> ..< <span style="color:rgb(39,139,210)">endIndex</span>) ~= idx ? <span style="color:rgb(211,54,130)">self</span>[idx] : <span style="color:rgb(211,54,130)">nil</span> }</div><div style="margin:0px;line-height:normal"> <span style="color:rgb(211,54,130)">set</span> { <span style="color:rgb(211,54,130)">if</span> (<span style="color:rgb(39,139,210)">startIndex</span> ..< <span style="color:rgb(39,139,210)">endIndex</span>) ~= idx && newValue != <span style="color:rgb(211,54,130)">nil</span> { <span style="color:rgb(211,54,130)">self</span>[idx] = newValue! } }</div><div style="margin:0px;line-height:normal"> }</div><div style="margin:0px;line-height:normal">}</div></div></div></div></blockquote></div><div><br></div><div> // array[index1] is only set if both indexes are valid</div><div> array[ifExists: index1] = array[ifExists: index2] </div><div><br></div><div><br></div><div>if array is of type [Int?] and the special setter for optional Elements would have been added:</div><div><br></div><div>array[index1] would be set to "nil" if array[index2] is nil <b style="text-decoration:underline">or</b> index2 is not valid which is unfortunate.</div></div></div></blockquote></div><br><div>Wouldn’t the return type be Int?? in this case? It’s not as pretty to test for as a plain Int? but iirc you can still distinguish a return type of nil from an optional that happens to contain nil, which should allow you to tell the difference between a nil value and an invalid index, I just can’t recall how at the moment (as I design around cases like these like my life depends on it ;)</div></div></blockquote></div></div></div><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>
<br></blockquote></div><br></div>