Thursday, January 24, 2019 [Tweets] [Favorites]

ExpressibleByStringInterpolation in Swift 5

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(…) 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!

For example, if you implement func appendInterpolation(_ string: String, pad: Int) that means that you’ll be able to build your type using an interpolation like: “Hello \(name, pad: 10), how are you?”. The interpolation just has to match one of the appendInterpolation signatures that your StringInterpolation subtype support.

Olivier Halligon:

In this second part, I’ll focus on one application of that new ExpressibleByStringInterpolation, to make NSAttributedString prettier.

Erica Sadun:

Consider printing optionals.

Erica Sadun:

Today’s extension enables (optionally padded) radix-based interpolation. You interpolate a number and specify a radix, the numerical base used to present it.

Erica Sadun:

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.

Ravi Kandhadai Madhavan (via Ben Cohen):

This post presents an overview of our plan for creating a customizable interface for Apple’s logging system. Our proposal uses custom string interpolation, which is a recently-improved language feature, and compile-time interpretation, which is a compiler technology that is in the process of being upstreamed as an experimental feature.

Update (2019-02-05): Mattt Thompson:

For a simple example of this, consider a custom type that escapes values in XML, similar to one of the loggers that we described last week. 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 < and >.

Update (2019-02-18): Mattt Thompson:

RegularExpressionDecoder provides a convenient solution to constructing Decodable objects from regular expression matches by automatically matching coding keys to capture group names. And it can do so safely, thanks to the new ExpressibleByStringInterpolation protocol in Swift 5.

Comments

Stay up-to-date by subscribing to the Comments RSS Feed for this post.

Leave a Comment