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)"