{"id":45693,"date":"2024-11-06T15:44:37","date_gmt":"2024-11-06T20:44:37","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=45693"},"modified":"2024-11-06T15:44:37","modified_gmt":"2024-11-06T20:44:37","slug":"swift-proposal-integer-generic-parameters","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2024\/11\/06\/swift-proposal-integer-generic-parameters\/","title":{"rendered":"Swift Proposal: Integer Generic Parameters"},"content":{"rendered":"<p><a href=\"https:\/\/github.com\/swiftlang\/swift-evolution\/blob\/main\/proposals\/0452-integer-generic-parameters.md\">SE-0452<\/a> (via <a href=\"https:\/\/discuss.systems\/@steve\/113432064730698565\">Steve Canon<\/a>):<\/p>\n<blockquote cite=\"https:\/\/github.com\/swiftlang\/swift-evolution\/blob\/main\/proposals\/0452-integer-generic-parameters.md\"><p>Swift does not currently support fixed-size or fixed-capacity collections\nwith inline storage. (Or at least, it doesn&rsquo;t do so <em>well<\/em>, not without\nforming a struct with some specific number of elements and doing horrible\nthings with <code>withUnsafePointer<\/code> to handle indexing.) Most of the implementation\nof something like a fixed-size array, or a fixed-capacity growable array with\na maximum size, or a hash table with a fixed number of buckets, is agnostic\nto any specific size or capacity, so that implementation\nwould ideally be generic over size so that a library implementation can\nbe reused for any given size.<\/p><p>Beyond inline storage sizes, there are other use cases for carrying integers\nin type information, such as to represent an operation with a particular\ninput or output size. Carrying this information in types can allow for APIs\nwith stronger static guarantees that chains of operations match in the\nnumber of elements they consume or produce.<\/p><p>[&#8230;]<\/p><p>Generic types can now be parameterized by integer parameters, declared using\nthe syntax <code>let &lt;Name&gt;: Int<\/code> inside of the generic parameter angle brackets:<\/p><pre>struct Vector&lt;let count: Int, Element&gt; {\n    \/*implementation TBD*\/\n}<\/pre><p>[&#8230;]<\/p><p>On platforms where the vendor ships the Swift runtime with the operating system, there may be limitations on using integer generic parameters in programs that want to target earlier versions of those platforms that don&rsquo;t have the necessary runtime support.<\/p><\/blockquote>\n<p>The <a href=\"https:\/\/github.com\/swiftlang\/swift-evolution\/blob\/main\/proposals\/0452-integer-generic-parameters.md#future-directions\">Future directions<\/a> section is interesting.<\/p>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2018\/06\/25\/open-sourcing-nonempty\/\">Open Sourcing NonEmpty<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2018\/01\/31\/swift-imports-fixed-size-c-arrays-as-tuples\/\">Swift Imports Fixed-size C Arrays As Tuples<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2015\/09\/03\/why-dependent-types-matter\/\">Why Dependent Types Matter<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2003\/06\/21\/types_in_microsoft_office\/\">Types in Microsoft Office<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>SE-0452 (via Steve Canon): Swift does not currently support fixed-size or fixed-capacity collections with inline storage. (Or at least, it doesn&rsquo;t do so well, not without forming a struct with some specific number of elements and doing horrible things with withUnsafePointer to handle indexing.) Most of the implementation of something like a fixed-size array, or [&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-11-06T20:44:39Z","apple_news_api_id":"7a4b08a7-70ae-4f72-a85b-10bcad996c00","apple_news_api_modified_at":"2024-11-06T20:44:39Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/AeksIp3CuT3KoWxC8rZlsAA","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,71,901],"class_list":["post-45693","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/45693","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=45693"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/45693\/revisions"}],"predecessor-version":[{"id":45694,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/45693\/revisions\/45694"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=45693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=45693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=45693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}