{"id":3140,"date":"2023-06-08T09:40:43","date_gmt":"2023-06-08T13:40:43","guid":{"rendered":"https:\/\/michaelrowe01.com\/?p=3140"},"modified":"2023-06-08T09:40:46","modified_gmt":"2023-06-08T13:40:46","slug":"generalize-apis-with-parameter-packs","status":"publish","type":"post","link":"https:\/\/michaelrowe01.com\/index.php\/blog\/generalize-apis-with-parameter-packs\/","title":{"rendered":"Generalize APIs with parameter packs"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"660\" height=\"371\" src=\"https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-15.png?resize=660%2C371&#038;ssl=1\" alt=\"\" class=\"wp-image-3141\" srcset=\"https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-15.png?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-15.png?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-15.png?resize=768%2C432&amp;ssl=1 768w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-15.png?resize=1536%2C864&amp;ssl=1 1536w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-15.png?w=1920&amp;ssl=1 1920w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-15.png?w=1320&amp;ssl=1 1320w\" sizes=\"auto, (max-width: 660px) 100vw, 660px\" \/><\/figure>\n\n\n\n<p>This is an advance topic and builds on last year\u2019s \u201c<strong>Embrace Swift Generics<\/strong>\u201d it is highly recommended that you review this before watching this session.<\/p>\n\n\n\n<p>What parameter packs solve<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>This is about generics and variadics<\/li>\n\n\n\n<li>Code is written in values and types.&nbsp; You can abstract over types by having varying types, most generic code abstracts over types and values<\/li>\n\n\n\n<li>If you want variable number of parameters .. you have Variadic parameters.\u00a0 But they have limitations, you can\u2019t know the number of parameters while preserving type information.\u00a0 The only way to do this now is via overloading.\n<ul class=\"wp-block-list\">\n<li>This pattern is limiting and adds redundancy.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>This solves the overloading pattern problem<\/li>\n<\/ul>\n\n\n\n<p>How to read parameter packs<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In code a parameter pack can handle any qty or types,&nbsp;<\/li>\n\n\n\n<li>A type pack handles different types<\/li>\n\n\n\n<li>A value pack handles different values<\/li>\n\n\n\n<li>They are used together &#8211; they are positional, so at the type of position 0 is related to the value at position 0.&nbsp; Think of it like a collection.<\/li>\n\n\n\n<li>You need to iterate to process, but each element can have a different type.&nbsp; So you would define a pack like &lt;each&nbsp; Type&gt; &#8211; this is a type parameter type.&nbsp; Use singular instead of plural. So you then use <strong>repeat Function&lt;each Type&gt;<\/strong> to see how this replacement works. You will get a comma separated list of types. They can only be used In things that naturally support comma separated lists, like function parameters, etc.<\/li>\n\n\n\n<li>Using in a type pack in a function value, it becomes a value pack<\/li>\n\n\n\n<li>You can add constraints to a parameter pack to enable conformance<\/li>\n\n\n\n<li>You an force a minimum number of arguments (even though the default is zero or more arguments).<\/li>\n<\/ul>\n\n\n\n<p>Using parameter packs<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>To use this code, you will want to use repetition patterns like <strong>repeat (each item).evaluate()<\/strong> if you want a list of all the values you can wrap in parenthesis like <strong>(repeat (each item).evaluate())<\/strong><\/li>\n\n\n\n<li>This should make your code simpler, and also allow you to write a lot less code<\/li>\n\n\n\n<li>To exit an iteration &#8211; use throwing methods to allow you to break out of the iteration loop (if needed)<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>This is an advance topic and builds on last year\u2019s \u201cEmbrace Swift Generics\u201d it is highly recommended that you review this before watching this session. What parameter packs solve How to read parameter packs Using parameter packs<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_wp_convertkit_post_meta":{"form":"-1","landing_page":"0","tag":"0","restrict_content":"0"},"hide_page_title":"","footnotes":""},"categories":[2,3],"tags":[578,708,680],"class_list":["post-3140","post","type-post","status-publish","format-standard","hentry","category-blog","category-personal-softwareandit","tag-apis","tag-parameter-packs","tag-wwdc23"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/posts\/3140","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/comments?post=3140"}],"version-history":[{"count":1,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/posts\/3140\/revisions"}],"predecessor-version":[{"id":3142,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/posts\/3140\/revisions\/3142"}],"wp:attachment":[{"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/media?parent=3140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/categories?post=3140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/tags?post=3140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}