<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">I agree. I like this proposal.<div class=""><br class=""></div><div class="">-- E</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Oct 11, 2016, at 3:38 PM, Xiaodi Wu 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=""><div dir="ltr" class="">Very elegant solution. Strong +1; no other feedback comes to mind atm.<div class="gmail_extra"><br class=""></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Tue, Oct 11, 2016 at 4:28 PM, Nate Cook via swift-evolution <span dir="ltr" class=""><<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';background-color:rgb(255,255,255)" class="">Introduction</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)" class="">This proposal addresses significant unexpected performance gaps when using dictionaries. It introduces type-specific collections for a <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">Dictionary</code> instance's <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">keys</code> <wbr class="">and <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">values</code> properties.</p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)" class="">New <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">DictionaryKeys</code> and <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">Diction<wbr class="">aryValues</code> collections provide efficient key lookup and mutable access to dictionary values, enabling updates to be performed in-place and allowing copy-on-write optimization of stored values.</p><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';background-color:rgb(255,255,255)" class=""><a id="m_-4540582609135303749user-content-motivation" class="m_-4540582609135303749anchor" href="https://gist.github.com/natecook1000/473720ba072fa5a0cd5e6c913de75fe1#motivation" style="box-sizing:border-box;background-color:transparent;color:rgb(64,120,192);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"><u class=""></u><u class=""></u><u class=""></u><u class=""></u></a>Motivation</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)" class="">This proposal address two problems:</p><ul style="box-sizing:border-box;padding-left:2em;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)" class=""><li style="box-sizing:border-box" class="">The Dictionary type <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">keys</code> implementation is inefficient, because <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">LazyMapCollection</code> <wbr class="">doesn't know how to forward lookups to the underlying dictionary storage.</li><li style="box-sizing:border-box;margin-top:0.25em" class="">Dictionaries do not offer value-mutating APIs. The mutating key-based subscript wraps values in an <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">Optional</code>. This prevents types with copy-on-write optimizations from recognizing they are singly referenced.</li></ul><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)" class="">This proposal uses the following <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">[String: [Int]]</code> dictionary to demonstrate these problems:</p><div class="m_-4540582609135303749highlight-source-swift m_-4540582609135303749highlight" style="box-sizing:border-box;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)"><pre style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;margin-top:0px;margin-bottom:0px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;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-break:normal" class=""><span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">var</span> dict <span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">=</span> [<span class="m_-4540582609135303749pl-s" style="box-sizing:border-box;color:rgb(24,54,145)"><span class="m_-4540582609135303749pl-pds" style="box-sizing:border-box">"</span>one<span class="m_-4540582609135303749pl-pds" style="box-sizing:border-box">"</span></span>: [<span class="m_-4540582609135303749pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">1</span>], <span class="m_-4540582609135303749pl-s" style="box-sizing:border-box;color:rgb(24,54,145)"><span class="m_-4540582609135303749pl-pds" style="box-sizing:border-box">"</span>two<span class="m_-4540582609135303749pl-pds" style="box-sizing:border-box">"</span></span>: [<span class="m_-4540582609135303749pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">2</span>, <span class="m_-4540582609135303749pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">2</span>], <span class="m_-4540582609135303749pl-s" style="box-sizing:border-box;color:rgb(24,54,145)"><span class="m_-4540582609135303749pl-pds" style="box-sizing:border-box">"</span>three<span class="m_-4540582609135303749pl-pds" style="box-sizing:border-box">"</span></span>: [<span class="m_-4540582609135303749pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">3</span>, <span class="m_-4540582609135303749pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">3</span>, <span class="m_-4540582609135303749pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">3</span>]]</pre></div><h3 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;font-size:1.25em;line-height:1.25;color:rgb(51,51,51);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';background-color:rgb(255,255,255)" class=""><a id="m_-4540582609135303749user-content-inefficient-dictkeys-search" class="m_-4540582609135303749anchor" href="https://gist.github.com/natecook1000/473720ba072fa5a0cd5e6c913de75fe1#inefficient-dictkeys-search" style="box-sizing:border-box;background-color:transparent;color:rgb(64,120,192);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"><u class=""></u><u class=""></u><u class=""></u><u class=""></u></a>Inefficient <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:inherit;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">dict.keys</code> Search</h3><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)" class="">Swift coders normally test key membership using <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">nil</code> checks or underscored optional bindings:</p><div class="m_-4540582609135303749highlight-source-swift m_-4540582609135303749highlight" style="box-sizing:border-box;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)"><pre style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;margin-top:0px;margin-bottom:0px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;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-break:normal" class=""><span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">if</span> dict[<span class="m_-4540582609135303749pl-s" style="box-sizing:border-box;color:rgb(24,54,145)"><span class="m_-4540582609135303749pl-pds" style="box-sizing:border-box">"</span>one<span class="m_-4540582609135303749pl-pds" style="box-sizing:border-box">"</span></span>] <span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">!=</span> <span class="m_-4540582609135303749pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">nil</span> {
<span class="m_-4540582609135303749pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">// ...</span>
}
<span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">if</span> <span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">let</span> _ <span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">=</span> dict[<span class="m_-4540582609135303749pl-s" style="box-sizing:border-box;color:rgb(24,54,145)"><span class="m_-4540582609135303749pl-pds" style="box-sizing:border-box">"</span>one<span class="m_-4540582609135303749pl-pds" style="box-sizing:border-box">"</span></span>] {
<span class="m_-4540582609135303749pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">// ...</span>
}</pre></div><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)" class="">These approaches provide the expected performance of a dictionary lookup but they read neither well nor "Swifty". Checking <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">keys</code> reads much better but introduces a serious performance penalty: this approach requires a linear search through a dictionary's keys to find a match.</p><pre style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;margin-top:0px;margin-bottom:16px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;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;color:rgb(51,51,51)" class=""><code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;padding:0px;margin:0px;background-color:transparent;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;word-break:normal;border:0px;display:inline;overflow:visible;line-height:inherit;word-wrap:normal" class="">if dict.keys.contains("one") {
// ...
}
</code></pre><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)" class="">A similar dynamic plays out when comparing <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">dict.index(forKey:)</code> <wbr class="">and <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">dict.keys.index(of:)</code>.</p><h3 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;font-size:1.25em;line-height:1.25;color:rgb(51,51,51);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';background-color:rgb(255,255,255)" class=""><a id="m_-4540582609135303749user-content-inefficient-value-mutation" class="m_-4540582609135303749anchor" href="https://gist.github.com/natecook1000/473720ba072fa5a0cd5e6c913de75fe1#inefficient-value-mutation" style="box-sizing:border-box;background-color:transparent;color:rgb(64,120,192);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"><u class=""></u><u class=""></u><u class=""></u><u class=""></u></a>Inefficient Value Mutation</h3><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)" class="">Dictionary values can be modified through the keyed subscript by direct reassignment or by using optional chaining. Both of these statements append <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">1</code> to the array stored by the key <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">"one"</code>:</p><div class="m_-4540582609135303749highlight-source-swift m_-4540582609135303749highlight" style="box-sizing:border-box;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)"><pre style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;margin-top:0px;margin-bottom:0px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;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-break:normal" class=""><span class="m_-4540582609135303749pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">// Direct re-assignment</span>
dict[<span class="m_-4540582609135303749pl-s" style="box-sizing:border-box;color:rgb(24,54,145)"><span class="m_-4540582609135303749pl-pds" style="box-sizing:border-box">"</span>one<span class="m_-4540582609135303749pl-pds" style="box-sizing:border-box">"</span></span>] <span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">=</span> (dict[<span class="m_-4540582609135303749pl-s" style="box-sizing:border-box;color:rgb(24,54,145)"><span class="m_-4540582609135303749pl-pds" style="box-sizing:border-box">"</span>one<span class="m_-4540582609135303749pl-pds" style="box-sizing:border-box">"</span></span>] <span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">??</span> []) <span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">+</span> [<span class="m_-4540582609135303749pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">1</span>]
<span class="m_-4540582609135303749pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">// Optional chaining</span>
dict[<span class="m_-4540582609135303749pl-s" style="box-sizing:border-box;color:rgb(24,54,145)"><span class="m_-4540582609135303749pl-pds" style="box-sizing:border-box">"</span>one<span class="m_-4540582609135303749pl-pds" style="box-sizing:border-box">"</span></span>]?<span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">.</span>append(<span class="m_-4540582609135303749pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">1</span>)</pre></div><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)" class="">Both approaches present problems. The first is complex and hard to read. The second ignores the case where <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">"one"</code> is not a key in the dictionary. It forces its check into a higher branch and encourages forced unwrapping. Furthermore, neither approach allows the array to grow in place. They introduce an unnecessary copy of the array's contents even though <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">dict</code> is the sole holder of its storage.</p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)" class="">Adding mutation to a dictionary's index-based subscripting isn't possible. Changing a key stored at a particular index would almost certainly modify its hash value, rendering the index incorrect. This violates the requirements of the <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">MutableCollection</code> <wbr class="">protocol.</p><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';background-color:rgb(255,255,255)" class=""><a id="m_-4540582609135303749user-content-proposed-solution" class="m_-4540582609135303749anchor" href="https://gist.github.com/natecook1000/473720ba072fa5a0cd5e6c913de75fe1#proposed-solution" style="box-sizing:border-box;background-color:transparent;color:rgb(64,120,192);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"><u class=""></u><u class=""></u><u class=""></u><u class=""></u></a>Proposed Solution</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)" class="">This proposal adds a custom collection for the <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">keys</code> and <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">values</code> dictionary properties. This follows the example set by <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">String</code>, which presents multiple views of its contents. A new <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">DictionaryKeys</code> collection introduces efficient key lookup, while a new <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">DictionaryValues</code> <wbr class="">collection provides a mutable collection interface to dictionary values.</p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)" class="">These changes introduce a simple and efficient way of checking whether a dictionary includes a key:</p><div class="m_-4540582609135303749highlight-source-swift m_-4540582609135303749highlight" style="box-sizing:border-box;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)"><pre style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;margin-top:0px;margin-bottom:0px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;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-break:normal" class=""><span class="m_-4540582609135303749pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">// Performant</span>
<span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">if</span> dict<span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">.</span><span class="m_-4540582609135303749pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">keys</span><span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">.</span><span class="m_-4540582609135303749pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">contains</span>(<span class="m_-4540582609135303749pl-s" style="box-sizing:border-box;color:rgb(24,54,145)"><span class="m_-4540582609135303749pl-pds" style="box-sizing:border-box">"</span>one<span class="m_-4540582609135303749pl-pds" style="box-sizing:border-box">"</span></span>) {
<span class="m_-4540582609135303749pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">// ...</span>
}</pre></div><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)" class="">As a mutable collection, <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">values</code> enables modification without copies or clumsy code:</p><div class="m_-4540582609135303749highlight-source-swift m_-4540582609135303749highlight" style="box-sizing:border-box;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)"><pre style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;margin-top:0px;margin-bottom:0px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;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-break:normal" class=""><span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">if</span> <span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">let</span> i <span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">=</span> dict<span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">.</span>index(forKey: <span class="m_-4540582609135303749pl-s" style="box-sizing:border-box;color:rgb(24,54,145)"><span class="m_-4540582609135303749pl-pds" style="box-sizing:border-box">"</span>one<span class="m_-4540582609135303749pl-pds" style="box-sizing:border-box">"</span></span>) {
dict<span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">.</span><span class="m_-4540582609135303749pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">values</span>[i]<span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">.</span>append(<span class="m_-4540582609135303749pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">1</span>) <span class="m_-4540582609135303749pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">// no copy here</span>
} <span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">else</span> {
dict[<span class="m_-4540582609135303749pl-s" style="box-sizing:border-box;color:rgb(24,54,145)"><span class="m_-4540582609135303749pl-pds" style="box-sizing:border-box">"</span>one<span class="m_-4540582609135303749pl-pds" style="box-sizing:border-box">"</span></span>] <span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">=</span> [<span class="m_-4540582609135303749pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">1</span>]
}</pre></div><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)" class="">Both the <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">keys</code> and <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">values</code> <wbr class="">collections share the same index type as <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">Dictionary</code>. This allows the above sample to be rewritten as:</p><div class="m_-4540582609135303749highlight-source-swift m_-4540582609135303749highlight" style="box-sizing:border-box;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)"><pre style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;margin-top:0px;margin-bottom:0px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;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-break:normal" class=""><span class="m_-4540582609135303749pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">// Using `dict.keys.index(of:)`</span>
<span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">if</span> <span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">let</span> i <span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">=</span> dict<span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">.</span><span class="m_-4540582609135303749pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">keys</span><span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">.</span>index(of: <span class="m_-4540582609135303749pl-s" style="box-sizing:border-box;color:rgb(24,54,145)"><span class="m_-4540582609135303749pl-pds" style="box-sizing:border-box">"</span>one<span class="m_-4540582609135303749pl-pds" style="box-sizing:border-box">"</span></span>) {
dict<span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">.</span><span class="m_-4540582609135303749pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">values</span>[i]<span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">.</span>append(<span class="m_-4540582609135303749pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">1</span>)
} <span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">else</span> {
dict[<span class="m_-4540582609135303749pl-s" style="box-sizing:border-box;color:rgb(24,54,145)"><span class="m_-4540582609135303749pl-pds" style="box-sizing:border-box">"</span>one<span class="m_-4540582609135303749pl-pds" style="box-sizing:border-box">"</span></span>] <span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">=</span> [<span class="m_-4540582609135303749pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">1</span>]
}</pre></div><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';background-color:rgb(255,255,255)" class=""><a id="m_-4540582609135303749user-content-detailed-design" class="m_-4540582609135303749anchor" href="https://gist.github.com/natecook1000/473720ba072fa5a0cd5e6c913de75fe1#detailed-design" style="box-sizing:border-box;background-color:transparent;color:rgb(64,120,192);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"><u class=""></u><u class=""></u><u class=""></u><u class=""></u></a>Detailed design</h2><ul style="box-sizing:border-box;padding-left:2em;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)" class=""><li style="box-sizing:border-box" class="">The standard library introduces two new collection types: <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">DictionaryKeys</code> and <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">Dict<wbr class="">ionaryValues</code>.</li><li style="box-sizing:border-box;margin-top:0.25em" class="">A <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">Dictionary</code>'s <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">keys</code> and <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">values</code><wbr class=""> property types change from <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">LazyMapCollection</code> to these new types. </li><li style="box-sizing:border-box;margin-top:0.25em" class="">The new collection types are not directly constructable. They are presented only as views into a dictionary.</li></ul><div class="m_-4540582609135303749highlight-source-swift m_-4540582609135303749highlight" style="box-sizing:border-box;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)"><pre style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;margin-top:0px;margin-bottom:0px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;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-break:normal" class=""><span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">struct</span> <span class="m_-4540582609135303749pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Dictionary</span><span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)"><</span>Key: <span class="m_-4540582609135303749pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Hashable</span>, Value<span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">></span>: <span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span> {
<span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">var</span> keys: DictionaryKeys<span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)"><</span>Key, Value<span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">></span> { <span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">get</span> }
<span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">var</span> values: DictionaryValues<span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)"><</span>Key, Value<span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">></span> { <span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">get</span> <span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">set</span> }
<span class="m_-4540582609135303749pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">// Remaining declarations</span>
}
<span class="m_-4540582609135303749pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">/// A collection view of a dictionary's keys.</span>
<span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">struct</span> DictionaryKeys<span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)"><</span>Key: <span class="m_-4540582609135303749pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Hashable</span>, Value<span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">></span>: Collection {
<span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">typealias</span> Index <span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">=</span> <span class="m_-4540582609135303749pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">DictionaryIndex</span><span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)"><</span>Key, Value<span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">></span>
<span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">subscript</span>(i: Index) <span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">-></span> Key { <span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">get</span> }
<span class="m_-4540582609135303749pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">// Other `Collection` requirements</span>
}
<span class="m_-4540582609135303749pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">/// A mutable collection view of a dictionary's values.</span>
<span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">struct</span> DictionaryValues<span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)"><</span>Key: <span class="m_-4540582609135303749pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Hashable</span>, Value<span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">></span>: MutableCollection {
<span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">typealias</span> Index <span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">=</span> <span class="m_-4540582609135303749pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">DictionaryIndex</span><span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)"><</span>Key, Value<span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">></span>
<span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">subscript</span>(i: Index) <span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">-></span> Value { <span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">get</span> <span class="m_-4540582609135303749pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">set</span> }
<span class="m_-4540582609135303749pl-c" style="box-sizing:border-box;color:rgb(150,152,150)">// Other `Collection` requirements</span>
}</pre></div><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)" class="">A sample implementation of this proposal can be found in <a href="https://github.com/natecook1000/swift/tree/nc-dictionary" style="box-sizing:border-box;background-color:transparent;color:rgb(64,120,192);text-decoration:none" target="_blank" class="">this branch</a>.</p><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';background-color:rgb(255,255,255)" class=""><a id="m_-4540582609135303749user-content-impact-on-existing-code" class="m_-4540582609135303749anchor" href="https://gist.github.com/natecook1000/473720ba072fa5a0cd5e6c913de75fe1#impact-on-existing-code" style="box-sizing:border-box;background-color:transparent;color:rgb(64,120,192);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"><u class=""></u><u class=""></u><u class=""></u><u class=""></u></a>Impact on existing code</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)" class="">The performance improvements of using the new <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">DictionaryKeys</code> type and the mutability of the <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">DictionaryValues</code>collection are both additive in nature.</p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)" class="">Most uses of these properties are transitory in nature. Adopting this proposal should not produce a major impact on existing code. The only impact on existing code exists where a program explicitly specifies the type of a dictionary's <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">keys</code>or <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">values</code> <wbr class="">property. The fix is to change the specified type. </p><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';background-color:rgb(255,255,255)" class=""><a id="m_-4540582609135303749user-content-alternatives-considered" class="m_-4540582609135303749anchor" href="https://gist.github.com/natecook1000/473720ba072fa5a0cd5e6c913de75fe1#alternatives-considered" style="box-sizing:border-box;background-color:transparent;color:rgb(64,120,192);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"><u class=""></u><u class=""></u><u class=""></u><u class=""></u></a>Alternatives considered</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)" class="">The <a href="https://github.com/apple/swift/blob/master/docs/GenericsManifesto.md" style="box-sizing:border-box;background-color:transparent;color:rgb(64,120,192);text-decoration:none" target="_blank" class="">Generics Manifesto</a> lists nested generics as a goal. This could impact the naming and structure of these new collection types. </p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255)" class="">Instead of <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">DictionaryKeys<Key, Value></code> and <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">DictionaryValues<<wbr class="">Key, Value></code>, these types could be <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">Dictionary<Key, Value>.Keys</code> and <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">Dictionary<<wbr class="">Key, Value>.Values</code>. However, because many types in the standard library may be revisited once such a feature is available (indices, iterators, etc.), the current lack of nesting shouldn't prevent consideration of this proposal.</p><div style="box-sizing:border-box;margin-top:0px;color:rgb(51,51,51);font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol';font-size:16px;background-color:rgb(255,255,255);margin-bottom:0px!important" class="">It could be possible to add additional compiler features that manage mutation through existing key-based subscripting without the copy-on-write problems of the current implementation. I don't know enough about how that would be implemented to speak to its feasibility or level of effort. Such a feature would reduce the need for a mutable <code style="box-sizing:border-box;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;padding:0.2em 0px;margin:0px;background-color:rgba(0,0,0,0.0392157);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px" class="">DictionaryValues</code> <wbr class="">collection.</div></div><br class="">______________________________<wbr class="">_________________<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" rel="noreferrer" target="_blank" class="">https://lists.swift.org/<wbr class="">mailman/listinfo/swift-<wbr class="">evolution</a><br class="">
<br class=""></blockquote></div><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="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></div></body></html>