{"id":37751,"date":"2022-11-29T16:12:00","date_gmt":"2022-11-29T21:12:00","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=37751"},"modified":"2022-11-29T16:12:00","modified_gmt":"2022-11-29T21:12:00","slug":"introduction-to-move-only-types-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2022\/11\/29\/introduction-to-move-only-types-in-swift\/","title":{"rendered":"Introduction to Move-Only Types in Swift"},"content":{"rendered":"<p><a href=\"https:\/\/forums.swift.org\/t\/an-informal-introduction-to-move-only-types\/61358\/1\">Tim Kientzle<\/a>:<\/p>\n<blockquote cite=\"https:\/\/forums.swift.org\/t\/an-informal-introduction-to-move-only-types\/61358\/1\"><p>I thought it would help to have an informal sketch to help outline why move-only types are interesting, clarify a few subtle points (like what &ldquo;move&rdquo; really means), and briefly explain some of the issues we&rsquo;ll need to tackle in order to bring this to Swift.<\/p>\n<p>[&#8230;]<\/p>\n<p>So the first step in bringing move-only support to Swift is to add operations with different lifetime-management behaviors. This will include constructs such as <code>for borrow x in collection<\/code> that let you iterate over the items in a collection without requiring an implicit copy and <code>f(take x)<\/code> that explicitly invalidates the local value as part of passing it into a function. We&rsquo;re also exploring variations of these that would allow you to temporarily gain mutable access to a value. These would allow you to efficiently mutate an element &ldquo;in place&rdquo; in various scenarios, which is a useful optimization tool for copyable values and an essential prerequisite for move-only values.<\/p>\n<p>[&#8230;]<\/p>\n<p>By making <code>Any<\/code> a synonym for <code>any Copyable<\/code>, we can ensure that <code>Any<\/code> is itself always copyable at the cost of limiting it to only store copyable values. This redefinition would preserve the behavior of current code that uses <code>Any<\/code>. Of course, this means we need to introduce a new type that can hold any value whether it is copyable or not.<\/p><\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2022\/08\/12\/swift-pitch-borrow-and-take-parameter-ownership-modifiers\/\">Swift Pitch: &ldquo;borrow&rdquo; and &ldquo;take&rdquo; Parameter Ownership Modifiers<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2022\/07\/26\/swift-proposal-move-function\/\">Swift Proposal: Move Function<\/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\/2020\/01\/24\/on-the-road-to-swift-6\/\">On the Road to Swift 6<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2017\/02\/18\/swift-ownership-manifesto\/\">Swift Ownership Manifesto<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Tim Kientzle: I thought it would help to have an informal sketch to help outline why move-only types are interesting, clarify a few subtle points (like what &ldquo;move&rdquo; really means), and briefly explain some of the issues we&rsquo;ll need to tackle in order to bring this to Swift. [&#8230;] So the first step in bringing [&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":"2022-11-29T21:12:02Z","apple_news_api_id":"e953a6c5-d516-470c-bc16-93a95fa624c8","apple_news_api_modified_at":"2022-11-29T21:12:02Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/A6VOmxdUWRwy8FpOpX6YkyA","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-37751","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\/37751","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=37751"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/37751\/revisions"}],"predecessor-version":[{"id":37752,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/37751\/revisions\/37752"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=37751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=37751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=37751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}