{"id":27624,"date":"2019-12-19T14:44:26","date_gmt":"2019-12-19T19:44:26","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=27624"},"modified":"2019-12-19T14:46:36","modified_gmt":"2019-12-19T19:46:36","slug":"swift-evolution-pitch-modify-accessors","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2019\/12\/19\/swift-evolution-pitch-modify-accessors\/","title":{"rendered":"Swift Evolution Pitch: Modify Accessors"},"content":{"rendered":"<p><a href=\"https:\/\/forums.swift.org\/t\/pitch-modify-accessors\/31872\">Ben Cohen<\/a>:<\/p>\n<blockquote cite=\"https:\/\/forums.swift.org\/t\/pitch-modify-accessors\/31872\">\n<p>We propose the introduction of a new keyword, <code>modify<\/code>, for implementing mutable computed properties and subscripts, alongside the current <code>get<\/code> and <code>set<\/code>.<\/p>\n<p>The bodies of <code>modify<\/code> implementations will be coroutines, and they will introduce a new contextual keyword, <code>yield<\/code>, that will be used to yield a value to be modified back to the caller. Control will resume after the <code>yield<\/code> when the caller returns.<\/p>\n<p>This <code>modify<\/code> feature is currently available (but not supported) from Swift 5.0 as <code>_modify<\/code>, for experimentation purposes when reviewing this proposal.<\/p>\n<p>[&#8230;]<\/p>\n<p>We cannot yield the value in the array&rsquo;s buffer directly because it needs to be placed inside an optional. That act of placing inside the optional creates a copy.<\/p>\n<p>We can work around this with some lower-level unsafe code. If the implementation of <code>Array.first<\/code> has access to its underlying buffer, it can move that value directly into the optional, yield it, and then move it back[&#8230;] During the yield to the caller, the array is in an invalid state: the memory location where the first element is stored is left uninitialized, and must not be accessed. This is safe due to Swift&rsquo;s rules preventing <a href=\"https:\/\/docs.swift.org\/swift-book\/LanguageGuide\/MemorySafety.html\">conflicting access to memory<\/a>.<\/p>\n<\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2019\/11\/02\/efficiently-mutating-nested-swift-data-structures\/\">Efficiently Mutating Nested Swift Data Structures<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2019\/02\/07\/fast-safe-mutable-state-in-swift-5\/\">Fast Safe Mutable State in Swift 5<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2019\/02\/05\/swift-5-exclusivity-enforcement\/\">Swift 5 Exclusivity Enforcement<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Ben Cohen: We propose the introduction of a new keyword, modify, for implementing mutable computed properties and subscripts, alongside the current get and set. The bodies of modify implementations will be coroutines, and they will introduce a new contextual keyword, yield, that will be used to yield a value to be modified back to 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,571,71,901],"class_list":["post-27624","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-memory-management","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/27624","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=27624"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/27624\/revisions"}],"predecessor-version":[{"id":27625,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/27624\/revisions\/27625"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=27624"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=27624"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=27624"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}