{"id":38783,"date":"2023-03-16T15:36:47","date_gmt":"2023-03-16T19:36:47","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=38783"},"modified":"2023-03-16T15:36:47","modified_gmt":"2023-03-16T19:36:47","slug":"passing-types-to-swift-functions","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2023\/03\/16\/passing-types-to-swift-functions\/","title":{"rendered":"Passing Types to Swift Functions"},"content":{"rendered":"<p><a href=\"https:\/\/paul-samuels.com\/blog\/2023\/01\/30\/providing-explicit-type-vs-type-inference-why-not-both\/\">Paul Samuels<\/a>:<\/p>\n<blockquote cite=\"https:\/\/paul-samuels.com\/blog\/2023\/01\/30\/providing-explicit-type-vs-type-inference-why-not-both\/\">\n<p>This post uses a toy helper function that fetches remote JSON to show how we can design its api so that explicitly providing the type isn&rsquo;t required when the compiler can infer types from context.<\/p>\n<p>[&#8230;]<\/p>\n<p>We can look at how <code>JSONDecode.decode<\/code> is defined to see how its api is designed. Clicking through the header we see<\/p>\n<pre>open func decode&lt;T&gt;(_ type: T.Type, from data: Data) throws -&gt; T where T : Decodable<\/pre>\n<\/blockquote>\n<p>This is nothing new, but I wanted to document it here because I did not find this pattern obvious when I first learned it. My instinct from Objective-C was to pass in an <code>AnyClass<\/code> or perhaps an <code>Any.Type<\/code>, but that doesn&rsquo;t work because there&rsquo;s no way to convince the compiler that the return value will match the type received at runtime.<\/p>\n<p>So you have to use generics. You could omit the <code>type<\/code> parameter and have the compiler infer <code>T<\/code> based on how the call site uses the return value, but that feels kind of backwards. Instead, using <code>T.Type<\/code> lets you go the other way and have <code>T<\/code> inferred from the parameter. This makes more sense, though it&rsquo;s still a bit weird because you are passing in <code>type<\/code> but don&rsquo;t really need it at runtime since you already have <code>T.self<\/code>.<\/p>\n<blockquote cite=\"https:\/\/paul-samuels.com\/blog\/2023\/01\/30\/providing-explicit-type-vs-type-inference-why-not-both\/\">\n<p>With the latest change we have more flexibility but if feels like we&rsquo;ve lost some brevity in cases where the compiler can infer things. To bring this type inference back we can use a default argument[&#8230;]<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Paul Samuels: This post uses a toy helper function that fetches remote JSON to show how we can design its api so that explicitly providing the type isn&rsquo;t required when the compiler can infer types from context. [&#8230;] We can look at how JSONDecode.decode is defined to see how its api is designed. Clicking through [&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":"2023-03-16T19:36:49Z","apple_news_api_id":"088697c5-3f27-4e02-8b15-5a2c7605fa15","apple_news_api_modified_at":"2023-03-16T19:36:49Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/ACIaXxT8nTgKLFVosdgX6FQ","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":[71,1774,901],"class_list":["post-38783","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-programming","tag-swift-codable","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/38783","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=38783"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/38783\/revisions"}],"predecessor-version":[{"id":38784,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/38783\/revisions\/38784"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=38783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=38783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=38783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}