<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="">Thanks for your feedback! Response below.<div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Oct 12, 2016, at 5:40 AM, Daniel Vollmer 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 class="">Hi,<br class=""><br class="">I very much think the points mentioned in the motivation are worth addressing (and IMO this is not an area where “maybe the optimizer can be made smarter” can cut it; I want performance guarantees, not hopes).<br class=""><br class=""><blockquote type="cite" class="">On 11 Oct 2016, at 23:28, Nate Cook via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""></blockquote><br class="">[snip]<br class=""><br class="">On a shallow read I like presented approach, except for<br class=""><br class=""><blockquote type="cite" class="">Both the keys and values collections share the same index type as Dictionary. This allows the above sample to be rewritten as:<br class=""><br class="">// Using `dict.keys.index(of:)`<br class="">if let i = dict.keys.index(of: "one") {<br class=""> dict.values[i].append(1)<br class="">} else {<br class=""> dict["one"] = [1]<br class="">}<br class=""></blockquote><br class="">The asymmetry between the if / else branches seems ugly to me. That is once obtaining the value “directly” from dict, and once through the values-view. I don’t have a great solution here, but is is possible to subscript the dict by its `Index` as well as its `Key`?<br class=""><br class="">```<br class="">// Using `dict.keys.index(of:)`<br class="">if let i = dict.keys.index(of: "one") {<br class=""> dict[i].append(1)<br class="">} else {<br class=""> dict["one"] = [1]<br class="">}<br class="">```<br class=""></div></div></blockquote><div><br class=""></div><div>I share your concern with this, and there is an approach that would make this kind of interface possible. Basically, what you're describing here would necessitate changing Dictionary to act like a mutable collection of values, and instead of presenting `keys` and `values` views, we would probably offer `keys` and `keysAndValues` (or something) views. With that new model, we'd have code like the following:</div><div><br class=""></div></div></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><div><div>var dict = ["one": [1], "two": [2, 2], "three": [3, 3, 3]]</div></div></div><div class=""><div><div><br class=""></div></div></div><div class=""><div><div>// Iterating the dictionary itself would be like we now iterate the values collection</div></div></div><div class=""><div><div>for val in dict {</div></div></div><div class=""><div><div><span class="Apple-tab-span" style="white-space:pre">        </span>print(val)</div></div></div><div class=""><div><div>}</div></div></div><div class=""><div><div>// Output is just the values (unordered as usual)</div></div></div><div class=""><div><div>// [1]</div></div></div><div class=""><div><div>// [3, 3, 3]</div></div></div><div class=""><div><div>// [2, 2]</div></div></div><div class=""><div><div><br class=""></div></div></div><div class=""><div><div>// Iterating a new view would act like the dictionary currently does</div></div></div><div class=""><div><div>for (key, val) in dict.keysAndValues {</div></div></div><div class=""><div><div><span class="Apple-tab-span" style="white-space:pre">        </span>print(key, val)</div></div></div><div class=""><div><div>}</div></div></div><div class=""><div><div>// "one", [1]</div></div></div><div class=""><div><div>// "three", [3, 3, 3]</div></div></div><div class=""><div><div>// "two", [2, 2]</div></div></div></blockquote><div class=""><div><div><br class=""></div><div>Any sequence or collections operations on the dictionary itself would only interact with values:</div><div><br class=""></div></div></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><div><div>print(dict.first)</div></div></div><div class=""><div><div>// Optional([1])</div></div></div><div class=""><div><div>print(dict.first(where: { $0.count > 2 }))</div></div></div><div class=""><div><div>// Optional([3, 3, 3])</div></div></div></blockquote><div class=""><div><br class=""></div><div>I'm not strictly opposed to this approach, but I do prefer the way the current dictionary implementation presents its elements as key-value pairs. When you iterate a dictionary you're seeing <i class="">all</i> of its contents, which I like, but there are clear tradeoffs between the two. Making Dictionary a collection of values is also a much more significant change than the one proposed—we'd need to do some research into the ways dictionaries are used to know how much larger an effect that would be.</div><div><br class=""></div><div>What do others think of this as an alternative solution for the motivating issues? Does anyone actually use indices right now to work with dictionaries, or is key-based read/write pretty much the only interface?</div><div><br class=""><blockquote type="cite" class=""><div class=""><div class="">On another note, I’m not sure whether there is a way (or whether it’s even worth trying) to avoid hashing the key twice when the `else` branch is taken.<br class=""></div></div></blockquote><div><br class=""></div><div>This is outside the scope of the proposal, but as far as I can see I don't think there's a solution for this that wouldn't overly expose the internal workings of the dictionary.</div></div><br class=""></div><div class="">Thanks again,</div><div class="">Nate</div><div class=""><br class=""></div><div class=""><br class=""></div></body></html>