{"id":24061,"date":"2019-01-24T16:22:39","date_gmt":"2019-01-24T21:22:39","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=24061"},"modified":"2022-06-10T16:35:35","modified_gmt":"2022-06-10T20:35:35","slug":"expressiblebystringinterpolation-in-swift-5","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2019\/01\/24\/expressiblebystringinterpolation-in-swift-5\/","title":{"rendered":"ExpressibleByStringInterpolation in Swift 5"},"content":{"rendered":"<p><a href=\"https:\/\/github.com\/apple\/swift-evolution\/blob\/master\/proposals\/0228-fix-expressiblebystringinterpolation.md\">SE-0228<\/a>:<\/p>\n<blockquote cite=\"https:\/\/github.com\/apple\/swift-evolution\/blob\/master\/proposals\/0228-fix-expressiblebystringinterpolation.md\">\n<p>String interpolation is a simple and powerful feature for expressing complex, runtime-created strings, but the current version of the <code>ExpressibleByStringInterpolation<\/code> protocol has been deprecated since Swift 3. We propose a new design that improves its performance, clarity, and efficiency.<\/p>\n<\/blockquote>\n\n<p><a href=\"http:\/\/alisoftware.github.io\/swift\/2018\/12\/15\/swift5-stringinterpolation-part1\/\">Olivier Halligon<\/a> (<a href=\"https:\/\/twitter.com\/aligatr\/status\/1074366773976317952\">tweet<\/a>):<\/p>\n<blockquote cite=\"http:\/\/alisoftware.github.io\/swift\/2018\/12\/15\/swift5-stringinterpolation-part1\/\"><p>The fact that you can implement whatever <code>appendInterpolation(&#8230;)<\/code> method you like means that you can choose what interpolation to support. This is a super powerful feature that opens a large range of possibilities!<\/p><p>For example, if you implement <code>func appendInterpolation(_ string: String, pad: Int)<\/code> that means that you&rsquo;ll be able to build your type using an interpolation like: <code>\"Hello \\(name, pad: 10), how are you?\"<\/code>. The interpolation just has to match one of the <code>appendInterpolation<\/code> signatures that your <code>StringInterpolation<\/code> subtype support.<\/p><\/blockquote>\n\n<p><a href=\"http:\/\/alisoftware.github.io\/swift\/2018\/12\/16\/swift5-stringinterpolation-part2\/\">Olivier Halligon<\/a>:<\/p>\n<blockquote cite=\"http:\/\/alisoftware.github.io\/swift\/2018\/12\/16\/swift5-stringinterpolation-part2\/\"><p>In this second part, I&rsquo;ll focus on one application of that new <code>ExpressibleByStringInterpolation<\/code>, to make <code>NSAttributedString<\/code> prettier.<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/ericasadun.com\/2018\/12\/12\/the-beauty-of-swift-5-string-interpolation\/\">Erica Sadun<\/a>:<\/p>\n<blockquote cite=\"https:\/\/ericasadun.com\/2018\/12\/12\/the-beauty-of-swift-5-string-interpolation\/\">\n<p>Consider printing optionals.<\/p>\n<\/blockquote>\n\n<p><a href=\"https:\/\/ericasadun.com\/2018\/12\/14\/more-fun-with-swift-5-string-interpolation-radix-formatting\/\">Erica Sadun<\/a>:<\/p>\n<blockquote cite=\"https:\/\/ericasadun.com\/2018\/12\/14\/more-fun-with-swift-5-string-interpolation-radix-formatting\/\">\n<p>Today&rsquo;s extension enables (optionally padded) radix-based interpolation. You interpolate a number and specify a radix, the numerical base used to present it.<\/p>\n<\/blockquote>\n\n<p><a href=\"https:\/\/ericasadun.com\/2018\/12\/16\/swift-5-interpolation-part-3-dates-and-number-formatters\/\">Erica Sadun<\/a>:<\/p>\n<blockquote cite=\"https:\/\/ericasadun.com\/2018\/12\/16\/swift-5-interpolation-part-3-dates-and-number-formatters\/\">\n<p>Consider formatters. Both Swift and Cocoa\/Cocoa touch support a number of these, ranging from numbers and currency to dates and times. They are a natural interpolation fit.<\/p>\n<\/blockquote>\n\n<p><a href=\"https:\/\/forums.swift.org\/t\/custom-string-interpolation-and-compile-time-interpretation-applied-to-logging\/18799\">Ravi Kandhadai Madhavan<\/a> (via <a href=\"https:\/\/twitter.com\/AirspeedSwift\/status\/1073436531166048256\">Ben Cohen<\/a>):<\/p>\n<blockquote cite=\"https:\/\/forums.swift.org\/t\/custom-string-interpolation-and-compile-time-interpretation-applied-to-logging\/18799\"><p>This post presents an overview of our plan for creating a customizable interface for Apple&rsquo;s logging system. Our proposal uses <a href=\"https:\/\/github.com\/apple\/swift-evolution\/blob\/master\/proposals\/0228-fix-expressiblebystringinterpolation.md\">custom string interpolation<\/a>, which is a recently-improved language feature, and <a href=\"https:\/\/forums.swift.org\/t\/compile-time-constant-expressions-for-swift\/12879\">compile-time interpretation<\/a>, which is a compiler technology that is in the process of being upstreamed as an <em>experimental<\/em> feature.<\/p><\/blockquote>\n\n<p id=\"expressiblebystringinterpolation-in-swift-5-update-2019-02-05\">Update (2019-02-05): <a href=\"https:\/\/nshipster.com\/expressiblebystringinterpolation\/\">Mattt Thompson<\/a>:<\/p>\n<blockquote cite=\"https:\/\/nshipster.com\/expressiblebystringinterpolation\/\">\n<p>For a simple example of this, consider a custom type that escapes values in XML, similar to one of the loggers <a href=\"https:\/\/nshipster.com\/textoutputstream\/#escaping-streamed-output\">that we described last week<\/a>. Our goal: to provide a nice templating API that allows us to write XML \/ HTML and interpolate values in a way that automatically escapes characters like <code>&lt;<\/code> and <code>&gt;<\/code>.<\/p>\n<\/blockquote>\n\n<p id=\"expressiblebystringinterpolation-in-swift-5-update-2019-02-18\">Update (2019-02-18): <a href=\"https:\/\/github.com\/Flight-School\/RegularExpressionDecoder\">Mattt Thompson<\/a>:<\/p>\n<blockquote cite=\"https:\/\/github.com\/Flight-School\/RegularExpressionDecoder\">\n<p><code>RegularExpressionDecoder<\/code> provides a convenient solution\nto constructing <code>Decodable<\/code> objects from regular expression matches\nby automatically matching coding keys to capture group names.\nAnd it can do so safely,\nthanks to the new <code>ExpressibleByStringInterpolation<\/code> protocol in Swift 5.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>SE-0228: String interpolation is a simple and powerful feature for expressing complex, runtime-created strings, but the current version of the ExpressibleByStringInterpolation protocol has been deprecated since Swift 3. We propose a new design that improves its performance, clarity, and efficiency. Olivier Halligon (tweet): The fact that you can implement whatever appendInterpolation(&#8230;) method you like means [&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-01-24T21:22:41Z","apple_news_api_id":"be381d41-ee73-4984-ac5a-0dc74fcc5195","apple_news_api_modified_at":"2022-06-10T20:35:39Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAg==","apple_news_api_share_url":"https:\/\/apple.news\/AvjgdQe5zSYSsWg3HT8xRlQ","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,46,30,1609,71,234,901],"class_list":["post-24061","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-ios","tag-ios-12","tag-languagedesign","tag-mac","tag-macos-10-14","tag-programming","tag-regex","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/24061","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=24061"}],"version-history":[{"count":4,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/24061\/revisions"}],"predecessor-version":[{"id":36183,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/24061\/revisions\/36183"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=24061"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=24061"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=24061"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}