{"id":39783,"date":"2023-06-16T14:36:10","date_gmt":"2023-06-16T18:36:10","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=39783"},"modified":"2023-06-16T14:36:10","modified_gmt":"2023-06-16T18:36:10","slug":"swift-proposal-init-accessors","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2023\/06\/16\/swift-proposal-init-accessors\/","title":{"rendered":"Swift Proposal: Init Accessors"},"content":{"rendered":"<p><a href=\"https:\/\/github.com\/apple\/swift-evolution\/blob\/main\/proposals\/0400-init-accessors.md\">SE-0400<\/a>:<\/p>\n<blockquote cite=\"https:\/\/github.com\/apple\/swift-evolution\/blob\/main\/proposals\/0400-init-accessors.md\"><p>Swift applies <a href=\"https:\/\/en.wikipedia.org\/wiki\/Definite_assignment_analysis\">definite initialization analysis<\/a> to stored properties, stored local variables, and variables with property wrappers. Definite initialization ensures that memory is initialized on all paths before it is accessed. A common pattern in Swift code is to use one property as backing storage for one or more computed properties, and abstractions like <a href=\"https:\/\/github.com\/apple\/swift-evolution\/blob\/main\/proposals\/0258-property-wrappers.md\">property wrappers<\/a> and <a href=\"https:\/\/github.com\/apple\/swift-evolution\/blob\/main\/proposals\/0389-attached-macros.md\">attached macros<\/a> help facilitate this pattern. Under this pattern, the backing storage is an implementation detail, and most code works with the computed property, including initializers.<\/p><p>[&#8230;]<\/p><p>The ad-hoc nature of property wrapper initializers mixed with an exact definite initialization pattern prevent property wrappers with additional arguments from being initialized out-of-line. Furthermore, property-wrapper-like macros cannot achieve the same initializer usability, because any backing storage variables added must be initialized directly instead of supporting initialization through computed properties. For example, the <a href=\"https:\/\/github.com\/apple\/swift-evolution\/blob\/main\/proposals\/0395-observability.md\"><code>@Observable<\/code> macro<\/a> applies a property-wrapper-like transform that turns stored properties into computed properties backed by the observation APIs, but it provides no way to write an initializer using the original property names like the programmer expects[&#8230;]<\/p><p>[&#8230;]<\/p><p>This proposal adds <em><code>init<\/code> accessors<\/em> to opt computed properties on types into definite initialization that subsumes initialization of a set of zero or more specified stored properties, which allows assigning to computed properties in the body of a type&rsquo;s initializer[&#8230;]<\/p><\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2017\/08\/31\/using-lazy-variables-to-work-around-swift-initialization-rules\/\">Using Lazy Variables to Work Around Swift Initialization Rules<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2014\/11\/25\/strange-tales-of-swift-initialization\/\">Strange Tales of Swift Initialization<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>SE-0400: Swift applies definite initialization analysis to stored properties, stored local variables, and variables with property wrappers. Definite initialization ensures that memory is initialized on all paths before it is accessed. A common pattern in Swift code is to use one property as backing storage for one or more computed properties, and abstractions like property [&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":"2023-06-16T18:36:12Z","apple_news_api_id":"5314890b-d971-4bc0-a49d-36f0b5213b91","apple_news_api_modified_at":"2023-06-16T18:36:12Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/AUxSJC9lxS8CknTbwtSE7kQ","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,941,71,901],"class_list":["post-39783","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-macros","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/39783","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=39783"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/39783\/revisions"}],"predecessor-version":[{"id":39784,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/39783\/revisions\/39784"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=39783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=39783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=39783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}