{"id":26177,"date":"2019-08-05T16:46:00","date_gmt":"2019-08-05T20:46:00","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=26177"},"modified":"2019-08-05T16:46:00","modified_gmt":"2019-08-05T20:46:00","slug":"phantom-types-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2019\/08\/05\/phantom-types-in-swift\/","title":{"rendered":"Phantom Types in Swift"},"content":{"rendered":"<p><a href=\"https:\/\/www.swiftbysundell.com\/posts\/phantom-types-in-swift\">John Sundell<\/a>:<\/p>\n<blockquote cite=\"https:\/\/www.swiftbysundell.com\/posts\/phantom-types-in-swift\">\n<p>When converting <code>Data<\/code> into a <code>String<\/code>, like we do above, we pass the encoding that we want the string to be decoded using &mdash; in this case <code>UTF8<\/code> &mdash; by passing a reference to that type itself. That&rsquo;s really interesting. If we dive a bit deeper, we can then see that the Swift standard library defines the <code>UTF8<\/code> type that we refer to above as a <em>case-less enum<\/em> within yet another <em>namespace-like<\/em> enum called <code>Unicode<\/code>[&#8230;]<\/p>\n<p>[&#8230;]<\/p>\n<p>Through the use of phantom types, the above two measurement values can&rsquo;t be mixed, since what kind of unit that each value is for is encoded into that value&rsquo;s type. That prevents us from accidentally passing a length to a function that accepts an angle, and vice versa &mdash; just like how we prevented document formats from being mixed up before.<\/p>\n<p>[&#8230;]<\/p>\n<p>Using phantom types is an incredibly powerful technique that can let us leverage the type system in order to validate different variants of a given value. While using phantom types usually makes an API more verbose, and does come with the added complexity of generics &mdash; when dealing with different formats and variants, it can let us reduce our reliance on run-time checks, and let the compiler perform those checks instead.<\/p>\n<\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2015\/02\/01\/phantom-types-and-typed-notification-observers\/\">Phantom Types and Typed Notification Observers<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>John Sundell: When converting Data into a String, like we do above, we pass the encoding that we want the string to be decoded using &mdash; in this case UTF8 &mdash; by passing a reference to that type itself. That&rsquo;s really interesting. If we dive a bit deeper, we can then see that the Swift [&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":"2019-08-05T20:46:03Z","apple_news_api_id":"8fb258e4-c81f-4336-9736-74cdc2f3ccd6","apple_news_api_modified_at":"2019-08-05T20:46:03Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/Aj7JY5MgfQzaXNnTNwvPM1g","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":[69,31,1610,30,1609,71,901],"class_list":["post-26177","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-ios","tag-ios-12","tag-mac","tag-macos-10-14","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/26177","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=26177"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/26177\/revisions"}],"predecessor-version":[{"id":26178,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/26177\/revisions\/26178"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=26177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=26177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=26177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}