{"id":38833,"date":"2023-03-22T15:42:59","date_gmt":"2023-03-22T19:42:59","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=38833"},"modified":"2023-03-22T15:42:59","modified_gmt":"2023-03-22T19:42:59","slug":"swift-proposal-noncopyable-structs-and-enums","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2023\/03\/22\/swift-proposal-noncopyable-structs-and-enums\/","title":{"rendered":"Swift Proposal: Noncopyable Structs and Enums"},"content":{"rendered":"<p><a href=\"https:\/\/github.com\/apple\/swift-evolution\/blob\/main\/proposals\/0390-noncopyable-structs-and-enums.md\">SE-0390<\/a>:<\/p>\n<blockquote cite=\"https:\/\/github.com\/apple\/swift-evolution\/blob\/main\/proposals\/0390-noncopyable-structs-and-enums.md\"><p>All currently existing types in Swift are <strong>copyable<\/strong>, meaning it is possible\nto create multiple identical, interchangeable representations of any value of\nthe type. However, copyable structs and enums are not a great model for\nunique resources. Classes by contrast <em>can<\/em> represent a unique resource,\nsince an object has a unique identity once initialized, and only references to\nthat unique object get copied. However, because the references to the object are\nstill copyable, classes always demand <em>shared ownership<\/em> of the resource. This\nimposes overhead in the form of heap allocation (since the overall lifetime of\nthe object is indefinite) and reference counting (to keep track of the number\nof co-owners currently accessing the object), and shared access often\ncomplicates or introduces unsafety or additional overhead into an object&rsquo;s\nAPIs. Swift does not yet have a mechanism for defining types that\nrepresent unique resources with <em>unique ownership<\/em>.<\/p><p>[&#8230;]<\/p><p>We propose to allow for <code>struct<\/code> and <code>enum<\/code> types to declare themselves as\n<em>noncopyable<\/em>, using a new syntax for suppressing implied generic constraints,\n<code>~Copyable<\/code>. Values of noncopyable type always have unique ownership, and\ncan never be copied (at least, not using Swift&rsquo;s implicit copy mechanism).\nSince values of noncopyable structs and enums have unique identities, they can\nalso have <code>deinit<\/code> declarations, like classes, which run automatically at the\nend of the unique instance&rsquo;s lifetime.<\/p><\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\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\/2022\/07\/26\/swift-proposal-move-function\/\">Swift Proposal: Move Function<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>SE-0390: All currently existing types in Swift are copyable, meaning it is possible to create multiple identical, interchangeable representations of any value of the type. However, copyable structs and enums are not a great model for unique resources. Classes by contrast can represent a unique resource, since an object has a unique identity once initialized, [&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-03-22T19:43:02Z","apple_news_api_id":"f6d55ac3-40db-4883-ab10-11e7528d3db8","apple_news_api_modified_at":"2023-03-22T19:43:02Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/A9tVaw0DbSIOrEBHnUo09uA","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,138,71,901],"class_list":["post-38833","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-memory-management","tag-optimization","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/38833","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=38833"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/38833\/revisions"}],"predecessor-version":[{"id":38834,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/38833\/revisions\/38834"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=38833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=38833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=38833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}