{"id":17265,"date":"2017-02-20T17:17:21","date_gmt":"2017-02-20T22:17:21","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=17265"},"modified":"2017-02-20T22:50:07","modified_gmt":"2017-02-21T03:50:07","slug":"provide-custom-collections-for-dictionary-keys-and-values","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2017\/02\/20\/provide-custom-collections-for-dictionary-keys-and-values\/","title":{"rendered":"Provide Custom Collections for Dictionary Keys and Values"},"content":{"rendered":"<p><a href=\"https:\/\/github.com\/apple\/swift-evolution\/blob\/master\/proposals\/0154-dictionary-key-and-value-collections.md\">SE-0154<\/a>:<\/p>\n<blockquote cite=\"https:\/\/github.com\/apple\/swift-evolution\/blob\/master\/proposals\/0154-dictionary-key-and-value-collections.md\">\n<p>This proposal address two problems:<\/p>\n<ul>\n<li>While a dictionary&rsquo;s <code>keys<\/code> collection is fine for iteration, its implementation is inefficient when looking up a specific key, because <code>LazyMapCollection<\/code> doesn&rsquo;t know how to forward lookups to the underlying dictionary storage.<\/li>\n<li>Dictionaries do not offer value-mutating APIs. The mutating key-based subscript wraps values in an <code>Optional<\/code>. This prevents types with copy-on-write optimizations from recognizing they are singly referenced.<\/li>\n<\/ul>\n<p>[&#8230;]<\/p>\n<p>Dictionary values can be modified through the keyed subscript by direct reassignment or by using optional chaining. Both of these statements append <code>1<\/code> to the array stored by the key <code>\"one\"<\/code>:<\/p>\n<pre>\/\/ Direct re-assignment\ndict[\"one\"] = (dict[\"one\"] ?? []) + [1]\n\n\/\/ Optional chaining\ndict[\"one\"]?.append(1)<\/pre>\n<p>Both approaches present problems.<\/p>\n<\/blockquote>\n<p>The proposed solution is an improvement but still seems a bit awkward.<\/p>\n\n<p>Update (2017-02-20): <a href=\"https:\/\/twitter.com\/AirspeedSwift\/status\/833807822034907136\">Airspeed Velocity<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/AirspeedSwift\/status\/833807822034907136\"><p>There are other changes likely for Swift 4 that would make this less awkward (e.g. to supply a default value when subscripting).<\/p>\n<p>It&rsquo;s just that this particular change was ABI impacting (changes the type of <code>Dictionary.Value<\/code>) so was proposed during stage 1.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>SE-0154: This proposal address two problems: While a dictionary&rsquo;s keys collection is fine for iteration, its implementation is inefficient when looking up a specific key, because LazyMapCollection doesn&rsquo;t know how to forward lookups to the underlying dictionary storage. Dictionaries do not offer value-mutating APIs. The mutating key-based subscript wraps values in an Optional. This prevents [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[46,138,71,901],"class_list":["post-17265","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-optimization","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/17265","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=17265"}],"version-history":[{"count":3,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/17265\/revisions"}],"predecessor-version":[{"id":17268,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/17265\/revisions\/17268"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=17265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=17265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=17265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}