{"id":36765,"date":"2022-08-12T16:39:15","date_gmt":"2022-08-12T20:39:15","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=36765"},"modified":"2022-08-12T16:39:15","modified_gmt":"2022-08-12T20:39:15","slug":"swift-pitch-borrow-and-take-parameter-ownership-modifiers","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2022\/08\/12\/swift-pitch-borrow-and-take-parameter-ownership-modifiers\/","title":{"rendered":"Swift Pitch: &ldquo;borrow&rdquo; and &ldquo;take&rdquo; Parameter Ownership Modifiers"},"content":{"rendered":"<p><a href=\"https:\/\/forums.swift.org\/t\/borrow-and-take-parameter-ownership-modifiers\/59581\">Joe Groff<\/a>:<\/p>\n<blockquote cite=\"https:\/\/forums.swift.org\/t\/borrow-and-take-parameter-ownership-modifiers\/59581\">\n<p>We propose new <code>borrow<\/code> and <code>take<\/code> parameter modifiers to allow developers to explicitly choose the ownership convention that a function uses to receive immutable parameters. This allows for fine-tuning of performance by reducing the number of ARC calls or copies needed to call a function, and provides a necessary prerequisite feature for move-only types to specify whether a function consumes a move-only value or not.<\/p>\n<p>[&#8230;]<\/p>\n<p>Looking to the future, as part of our <a href=\"https:\/\/forums.swift.org\/t\/manifesto-ownership\/5212\">ongoing project to add ownership to Swift<\/a>, we will eventually have move-only values and types. Since move-only types do not have the ability to be copied, the distinction between the two conventions becomes an important part of the API contract: functions that <em>borrow<\/em> move-only values make temporary use of the value and leave it valid for further use, like reading from a file handle, whereas functions that <em>take<\/em> a move-only value consume it and prevent its further use, like closing a file handle. Relying on implicit selection of the parameter convention will not suffice for these types.<\/p>\n<\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\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\/2017\/02\/18\/swift-ownership-manifesto\/\">Swift Ownership Manifesto<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Joe Groff: We propose new borrow and take parameter modifiers to allow developers to explicitly choose the ownership convention that a function uses to receive immutable parameters. This allows for fine-tuning of performance by reducing the number of ARC calls or copies needed to call a function, and provides a necessary prerequisite feature for move-only [&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-08-12T20:39:17Z","apple_news_api_id":"124da660-9b79-43c1-b52d-29ef5470fc92","apple_news_api_modified_at":"2022-08-12T20:39:17Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/AEk2mYJt5Q8G1LSnvVHD8kg","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":[55,46,571,138,71,901],"class_list":["post-36765","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-arc","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\/36765","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=36765"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/36765\/revisions"}],"predecessor-version":[{"id":36766,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/36765\/revisions\/36766"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=36765"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=36765"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=36765"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}