How to add new string interpolations to an existing type?

That option is probably ill‐advised. One of the main reasons to create and use a conformer besides DefaultStringInterpolation is in order to restrict the available interpolations at compile time as a correctness aid in a particular situation. By extending every such type wholesale, you may be shooting yourself in the foot. Instead I would create a new protocol that refines it, and conform whatever interpolation engines you care about to that protocol:

protocol RadixInterpolation: StringInterpolationProtocol where StringLiteralType == String {}
extension RadixInterpolation {
  mutating func appendInterpolation(_ n: Int, radix: Int) {
    // ...
  }
}

extension DefaultStringInterpolation: RadixInterpolation {}
extension HelpfulInterpolation: RadixInterpolation {}
extension CustomInterpolation: RadixInterpolation {}
extension MyInterpolation: RadixInterpolation {}
// But no extension to `StrictInterpolation` or `SafeInterpolation`,
// because that would be counterproductive.

Edit: On the other hand, I guess if the extension isn’t public anyway, and you don’t expect to use a safety interpolator anywhere in your own code, then the extra effort probably isn’t worth it.

4 Likes