Is there any less verbose way to customize nil-valued optional types in string interpolation?

As of Swift 5, you could extend string interpolation:

extension String.StringInterpolation {
  mutating func appendInterpolation<T>(maybe: T?) {
    if let value = maybe {
      appendInterpolation(value)
    } else {
      appendLiteral("nil")
    }
  }
}

let x: Int? = nil

"\(maybe: x)"
8 Likes