{"id":42653,"date":"2024-03-27T14:42:47","date_gmt":"2024-03-27T18:42:47","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=42653"},"modified":"2024-03-27T14:42:47","modified_gmt":"2024-03-27T18:42:47","slug":"noncopyable-generics-walkthrough","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2024\/03\/27\/noncopyable-generics-walkthrough\/","title":{"rendered":"Noncopyable Generics Walkthrough"},"content":{"rendered":"<p><a href=\"https:\/\/forums.swift.org\/t\/noncopyable-generics-in-swift-a-code-walkthrough\/70862\">Ben Cohen<\/a>:<\/p>\n<blockquote cite=\"https:\/\/forums.swift.org\/t\/noncopyable-generics-in-swift-a-code-walkthrough\/70862\"><p>Non-copyable generics aren&rsquo;t for every-day code &#x2013; but we&rsquo;ve put a lot of care into making them stay out of your way until you need them, and then keeping them usable once you do. They will allow libraries to unlock more performance and safety for end users.<\/p><p>[&#8230;]<\/p><p>To help tie all these pieces together, I wrote up some code that uses all these proposals in order to build a basic singly-linked list type.<\/p><p>[&#8230;]<\/p><p>This is a struct that opts out of the default <code>Copyable<\/code> conformance via <code>: ~Copyable<\/code>. This allows it to have a <code>deinit<\/code>, like a class. This type uses no reference counting to know when to destroy the box. The type cannot be copied, so when it goes out of scope, the <code>deinit<\/code> is called by the compiler.<\/p><p>[&#8230;]<\/p><p>The generic placeholder <code>Wrapped<\/code>, which can stand in for the type of anything you want to put in the box, is <em>also<\/em> marked <code>~Copyable<\/code>. This means that the <code>Box<\/code> type cannot make any copies of its wrapped type. [&#8230;] What this <code>~Copyable<\/code> annotation means is just that the <code>Box<\/code> type doesn&rsquo;t <em>know<\/em> if the type it holds is copyable, which means it can safely hold both copyable and non-copyable types.<\/p><p>[&#8230;]<\/p><p><code>Sequence<\/code>, and therefore <code>for&#8230;in<\/code>, does not yet support non-copyable types. <code>Sequence<\/code> could be made to support it today by marking the protocol up as <code>~Copyable<\/code> and having <code>makeIterator()<\/code> be consuming. However this is probably not desirable. Mostly, you want iteration to be a borrowing operation. Accomplishing this needs more language features.<\/p><\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2023\/09\/19\/swift-5-9\/\">Swift 5.9<\/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\/07\/01\/porting-graphing-calculator-from-c-to-swift\/\">Porting Graphing Calculator From C++ to Swift<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Ben Cohen: Non-copyable generics aren&rsquo;t for every-day code &#x2013; but we&rsquo;ve put a lot of care into making them stay out of your way until you need them, and then keeping them usable once you do. They will allow libraries to unlock more performance and safety for end users.[&#8230;]To help tie all these pieces together, [&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-03-27T18:42:50Z","apple_news_api_id":"24a7bbfc-a7e9-4970-9140-6633f56905c9","apple_news_api_modified_at":"2024-03-27T18:42:50Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/AJKe7_KfpSXCRQGYz9WkFyQ","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-42653","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\/42653","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=42653"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/42653\/revisions"}],"predecessor-version":[{"id":42654,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/42653\/revisions\/42654"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=42653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=42653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=42653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}