{"id":17079,"date":"2017-01-31T14:23:37","date_gmt":"2017-01-31T19:23:37","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=17079"},"modified":"2017-01-31T14:23:37","modified_gmt":"2017-01-31T19:23:37","slug":"fun-with-swift-string-interpolation","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2017\/01\/31\/fun-with-swift-string-interpolation\/","title":{"rendered":"Fun With Swift String Interpolation"},"content":{"rendered":"<p><a href=\"https:\/\/oleb.net\/blog\/2017\/01\/fun-with-string-interpolation\/\">Ole Begemann<\/a>:<\/p>\n<blockquote cite=\"https:\/\/oleb.net\/blog\/2017\/01\/fun-with-string-interpolation\/\"><p>The source of the problem is that &ldquo;unsafe strings&rdquo; and &ldquo;safe strings&rdquo; are so fundamentally different that we should often treat them differently, yet we tend to use the same <a href=\"https:\/\/developer.apple.com\/reference\/swift\/string\"><code>String<\/code><\/a> type for both. So let&rsquo;s introduce separate types for these concepts. I&rsquo;m calling these <code>UnsafeString<\/code> and <code>SanitizedHTML<\/code>.<\/p><p>[&#8230;]<\/p><p>If we make sure that all rendering APIs only accept SanitizedHTML as input, the new types make it impossible to accidentally render an unescaped string.<\/p><p>[&#8230;]<\/p><p>Customizing how your own types interpret an interpolation string is an extremely powerful feature, especially for <a href=\"https:\/\/en.wikipedia.org\/wiki\/Domain-specific_language\">DSLs<\/a>. Building SQL queries or localized strings are just two examples where you could employ the same techniques (<a href=\"https:\/\/gist.github.com\/brentdax\/79fa038c0af0cafb52dd\">here&rsquo;s an implementation of the latter<\/a> by Brent Royal-Gordon). Any task that needs strings built from components can probably profit from it.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Ole Begemann: The source of the problem is that &ldquo;unsafe strings&rdquo; and &ldquo;safe strings&rdquo; are so fundamentally different that we should often treat them differently, yet we tend to use the same String type for both. So let&rsquo;s introduce separate types for these concepts. I&rsquo;m calling these UnsafeString and SanitizedHTML.[&#8230;]If we make sure that all [&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":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","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-17079","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\/17079","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=17079"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/17079\/revisions"}],"predecessor-version":[{"id":17080,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/17079\/revisions\/17080"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=17079"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=17079"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=17079"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}