{"id":46228,"date":"2024-12-19T16:07:20","date_gmt":"2024-12-19T21:07:20","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=46228"},"modified":"2024-12-19T16:07:20","modified_gmt":"2024-12-19T21:07:20","slug":"swift-prospective-vision-accessors","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2024\/12\/19\/swift-prospective-vision-accessors\/","title":{"rendered":"Swift Prospective Vision: Accessors"},"content":{"rendered":"<p><a href=\"https:\/\/github.com\/rjmccall\/swift-evolution\/blob\/accessors-vision\/visions\/accessors.md\">John McCall<\/a> (<a href=\"https:\/\/forums.swift.org\/t\/prospective-vision-accessors\/76707\">forum<\/a>):<\/p>\n<blockquote cite=\"https:\/\/github.com\/rjmccall\/swift-evolution\/blob\/accessors-vision\/visions\/accessors.md\"><p>However, this approach has significant problems. The biggest is that the <code>get<\/code> accessor has to return an independent value. If the accessor is just returning a value stored in memory, which is very common for data structures, this means the value has to be copied.<\/p><p>[&#8230;]<\/p><p>This vision document lays out the design space of accessors for the next few years, as Swift continues to advance its support for non-<code>Copyable<\/code> and non-<code>Escapable<\/code> types. It explains Swift&rsquo;s basic access model and how it may need to evolve. It explores what developers need from accessors in these advanced situations. Finally, it discusses different kinds of accessors, both existing and under consideration, and how they do or do not fit into the future of the language as we see it.<\/p><p>[&#8230;]<\/p><p>The <code>yield<\/code> accessor is the natural most-general model of a borrowing read access. It is a coroutine function which yields a borrowed value and can then do arbitrary finalization when resumed.<\/p><p>The <code>borrow<\/code> accessor is a specialization of that model which expresses that no finalization is required. It is an ordinary function that returns a borrowed value.<\/p><p>[&#8230;]<\/p><p><code>yield inout<\/code> and <code>mutate<\/code> are both modification accessors.<\/p><p>[&#8230;]<\/p><p><code>unsafeAddress<\/code> is functionally a borrowing read accessor. It is an ordinary function that returns an <code>UnsafePointer&lt;ValueType&gt;<\/code>.<\/p><\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2024\/03\/27\/noncopyable-generics-walkthrough\/\">Noncopyable Generics Walkthrough<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2023\/06\/16\/swift-proposal-init-accessors\/\">Swift Proposal: Init Accessors<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2023\/03\/22\/swift-proposal-noncopyable-structs-and-enums\/\">Swift Proposal: Noncopyable Structs and Enums<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2022\/11\/29\/introduction-to-move-only-types-in-swift\/\">Introduction to Move-Only Types in Swift<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2021\/12\/23\/roadmap-for-improving-swift-performance-predictability\/\">Roadmap for Improving Swift Performance Predictability<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2019\/12\/19\/swift-evolution-pitch-modify-accessors\/\">Swift Evolution Pitch: Modify Accessors<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>John McCall (forum): However, this approach has significant problems. The biggest is that the get accessor has to return an independent value. If the accessor is just returning a value stored in memory, which is very common for data structures, this means the value has to be copied.[&#8230;]This vision document lays out the design space [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2024-12-19T21:07:22Z","apple_news_api_id":"e60ed80e-5a77-41a2-b270-58561d693ba3","apple_news_api_modified_at":"2024-12-19T21:07:22Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/A5g7YDlp3QaKycFhWHWk7ow","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-46228","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\/46228","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=46228"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/46228\/revisions"}],"predecessor-version":[{"id":46229,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/46228\/revisions\/46229"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=46228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=46228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=46228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}