Appending literals in string interpolation

public mutating func appendLiteral(_ literal: String)'s documentation says:

Don't call this method directly; it's used by the compiler when interpreting string interpolations.

Is it just a clarification that the compiler uses it when type is ExpressibleByStringInterpolation or are we forbidden from using it?

My case is that I have this localized string in a localization table with "" key.

var case = "a"

Now, this LocalizedStringKey.StringInterpolation would build the localization key with %@ specifier with "a" parameter - as expected, not what I need though. I'm curious if I could go ahead and extend it like

extension LocalizedStringKey.StringInterpolation {

    mutating func appendInterpolation(asLiteral string: String) {

so Text("\(asLiteral: case).label") would translate with "" key. It works, but I'm concerned that I'm violating something and it might backfire on me in the future. If so, is there a solution for this - to build a literal key without format specifiers with interpolated non-literal values (of LocalizedStringKey)?

I believe that “defining new interpolations” is exactly when you should call appendLiteral.

For example, see the recent proposal for default-value optional interpolation.

1 Like