{"id":17233,"date":"2017-02-16T16:17:26","date_gmt":"2017-02-16T21:17:26","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=17233"},"modified":"2017-02-16T16:17:26","modified_gmt":"2017-02-16T21:17:26","slug":"being-a-mutable-collection-is-not-sufficient-to-be-a-mutablecollection","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2017\/02\/16\/being-a-mutable-collection-is-not-sufficient-to-be-a-mutablecollection\/","title":{"rendered":"Being a Mutable Collection is not Sufficient to be a MutableCollection"},"content":{"rendered":"<p><a href=\"https:\/\/oleb.net\/blog\/2017\/02\/why-is-dictionary-not-a-mutablecollection\/\">Ole Begemann<\/a>:<\/p>\n<blockquote cite=\"https:\/\/oleb.net\/blog\/2017\/02\/why-is-dictionary-not-a-mutablecollection\/\">\n<p>A <a href=\"https:\/\/developer.apple.com\/reference\/swift\/mutablecollection\"><code>MutableCollection<\/code><\/a> supports in-place element mutation. The single new API requirement it adds to <code>Collection<\/code> is that the <a href=\"https:\/\/developer.apple.com\/reference\/swift\/mutablecollection\/1640969-subscript\"><code>subscript<\/code><\/a> now must also have a setter.<\/p>\n<p>[&#8230;]<\/p>\n<p><code>MutableCollection<\/code> allows changing the values of a collection&rsquo;s elements, but the protocol&#x2BC;s documentation stipulates that the mutation must neither change the length of the collection nor the order of the elements. <code>Set<\/code> can&#x2BC;t satisfy either of these requirements.<\/p>\n<p>[&#8230;]<\/p>\n<p>All <code>Dictionary<\/code> would gain from conforming to <code>MutableCollection<\/code> and\/or <code>RangeReplaceableCollection<\/code> would be methods that operate on <code>Index<\/code> values and <code>(Key, Value)<\/code> pairs, which is probably not compelling enough to invest anything in the conformance even if it were compatible with the type&#x2BC;s implementation.<\/p>\n<\/blockquote>\n\n<p><a href=\"https:\/\/oleb.net\/blog\/2017\/02\/why-is-string-characterview-not-a-mutablecollection\/\">Ole Begemann<\/a>:<\/p>\n<blockquote cite=\"https:\/\/oleb.net\/blog\/2017\/02\/why-is-string-characterview-not-a-mutablecollection\/\">\n<p><code>CharacterView<\/code> does conform to <a href=\"https:\/\/developer.apple.com\/reference\/swift\/rangereplaceablecollection\"><code>RangeReplaceableCollection<\/code><\/a> but not to <a href=\"https:\/\/developer.apple.com\/reference\/swift\/mutablecollection\"><code>MutableCollection<\/code><\/a>. Why? A string is clearly mutable; it seems logical that it should adopt this protocol. Again, we need to consider the protocol&#x2BC;s <a href=\"https:\/\/oleb.net\/blog\/2016\/12\/protocols-have-semantics\/\">semantics<\/a>.<\/p>\n<p>[&#8230;]<\/p>\n<p>However, the <code>Character<\/code>&#x2BC;s <em>size<\/em> in the underlying storage <a href=\"\/blog\/2016\/08\/swift-3-strings\/\">is not the same<\/a> for <a href=\"https:\/\/oleb.net\/blog\/2016\/12\/emoji-4-0\/\">all characters<\/a>, so replacing a single <code>Character<\/code> can potentially make it necessary to move the subsequent text forward or backward in memory by a few bytes to make room for the replacement. This would make the simple subscript assignment potentially an <a href=\"https:\/\/en.wikipedia.org\/wiki\/Big_O_notation\">O(<em>n<\/em>)<\/a> operation, and subscripting is supposed to be O(1).<\/p>\n<p>[&#8230;]<\/p>\n<p>The final potential issue for <code>CharacterView<\/code>&#x2BC;s hypothetical <code>MutableCollection<\/code> conformance is <a href=\"https:\/\/en.wikipedia.org\/wiki\/Unicode\">Unicode<\/a> and the complexities it brings. The existence of <a href=\"https:\/\/en.wikipedia.org\/wiki\/Combining_character\">combining characters<\/a> means that replacing a single <code>Character<\/code> can actually change the string&#x2BC;s length (measured in <code>Character<\/code>s) if the new character combines with its preceding character.<\/p>\n<\/blockquote>\n<p>Previously: <a href=\"http:\/\/mjtsai.com\/blog\/2017\/01\/27\/swift-4-string-manifesto\/\">Swift 4 String Manifesto<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Ole Begemann: A MutableCollection supports in-place element mutation. The single new API requirement it adds to Collection is that the subscript now must also have a setter. [&#8230;] MutableCollection allows changing the values of a collection&rsquo;s elements, but the protocol&#x2BC;s documentation stipulates that the mutation must neither change the length of the collection nor the [&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,71,901,258],"class_list":["post-17233","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-programming","tag-swift-programming-language","tag-unicode"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/17233","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=17233"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/17233\/revisions"}],"predecessor-version":[{"id":17234,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/17233\/revisions\/17234"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=17233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=17233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=17233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}