{"id":46771,"date":"2025-02-17T08:57:19","date_gmt":"2025-02-17T13:57:19","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=46771"},"modified":"2025-02-17T08:57:19","modified_gmt":"2025-02-17T13:57:19","slug":"swift-proposal-inlinearray","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2025\/02\/17\/swift-proposal-inlinearray\/","title":{"rendered":"Swift Proposal: InlineArray"},"content":{"rendered":"<p><a href=\"https:\/\/github.com\/swiftlang\/swift-evolution\/blob\/main\/proposals\/0453-vector.md\">SE-0453<\/a>:<\/p>\n<blockquote cite=\"https:\/\/github.com\/swiftlang\/swift-evolution\/blob\/main\/proposals\/0453-vector.md\"><p>This proposal introduces a new type to the standard library, <code>InlineArray<\/code>, which is\na fixed-size array. This is analogous to the\n<a href=\"https:\/\/en.cppreference.com\/w\/c\/language\/array\">classical C arrays <code>T[N]<\/code><\/a>,\n<a href=\"https:\/\/en.cppreference.com\/w\/cpp\/container\/array\">C++&rsquo;s <code>std::array&lt;T, N&gt;<\/code><\/a>,\nand <a href=\"https:\/\/doc.rust-lang.org\/std\/primitive.array.html\">Rust&rsquo;s arrays <code>[T; N]<\/code><\/a>.<\/p><p>[&#8230;]<\/p><p>It&rsquo;s important to understand that <code>Array<\/code> is a heap allocated growable data\nstructure which can be expensive and unnecessary in some situations. The next\nbest thing is to force a known quantity of elements onto the stack, probably by\nusing tuples.<\/p><p>[&#8230;]<\/p><p>We introduce a new top level type, <code>InlineArray<\/code>, to the standard library which is a\nfixed-size contiguously inline allocated array. We&rsquo;re defining &ldquo;inline&rdquo; as using\nthe most natural allocation pattern depending on the context of where this is\nused. It will be stack allocated most of the time, but as a class property\nmember it will be inline allocated on the heap with the rest of the properties.\n<code>InlineArray<\/code> will never introduce an implicit heap allocation just for its storage\nalone.<\/p><p>[&#8230;]<\/p><p><code>InlineArray<\/code> will be a simple noncopyable struct capable of storing other potentially\nnoncopyable elements. It will be conditionally copyable only when its elements\nare.<\/p><\/blockquote>\n<p>Initialization is special-cased to avoid constructing a temporary array from the literal, and it does <em>not<\/em> conform to <code>Sequence<\/code> or <code>Collection<\/code>.<\/p>\n<blockquote cite=\"https:\/\/github.com\/swiftlang\/swift-evolution\/blob\/main\/proposals\/0453-vector.md\"><p>We do plan to propose new protocols that look like <code>Sequence<\/code> and <code>Collection<\/code>\nthat avoid implicit copying making them suitable for types like <code>InlineArray<\/code> and\ncontainers of noncopyable elements.\n<a href=\"https:\/\/github.com\/swiftlang\/swift-evolution\/blob\/main\/proposals\/0437-noncopyable-stdlib-primitives.md\">SE-0437 Noncopyable Standard Library Primitives<\/a>\ngoes into more depth about this rationale and mentions that creating new\nprotocols to support noncopyable containers with potentially noncopyable\nelements are all marked as future work.<\/p>\n<p>[&#8230;]<\/p>\n<p>With the introduction of <code>InlineArray<\/code>, we have a unique opportunity to fix another\npain point within the language with regards to C interop. Currently, the Swift\ncompiler imports a C array of type <code>T[24]<\/code> as a tuple of <code>T<\/code> with 24 elements.<\/p><\/blockquote>\n<p>But they haven&rsquo;t decided how to do that yet without breaking compatibility.<\/p>\n\n<p>Previously:<\/p>\n<ul>\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>SE-0453: This proposal introduces a new type to the standard library, InlineArray, which is a fixed-size array. This is analogous to the classical C arrays T[N], C++&rsquo;s std::array&lt;T, N&gt;, and Rust&rsquo;s arrays [T; N].[&#8230;]It&rsquo;s important to understand that Array is a heap allocated growable data structure which can be expensive and unnecessary in some situations. [&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":"2025-02-17T13:57:21Z","apple_news_api_id":"326bddf6-7275-44cb-86fe-e8e7977f2bdd","apple_news_api_modified_at":"2025-02-17T13:57:21Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/AMmvd9nJ1RMuG_ujnl38r3Q","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":[45,46,138,71,901],"class_list":["post-46771","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-c","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\/46771","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=46771"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/46771\/revisions"}],"predecessor-version":[{"id":46772,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/46771\/revisions\/46772"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=46771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=46771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=46771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}