Skip to content

Instantly share code, notes, and snippets.

Created May 21, 2021 07:46
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Custom Swift String Interpolation: Logs Privacy
struct Message {
enum Privacy {
case `public`
case `private`
var value: String
extension Message: CustomStringConvertible {
var description: String { value }
extension Message: ExpressibleByStringLiteral {
init(stringLiteral value: StringLiteralType) {
self.init(value: value)
extension Message: ExpressibleByStringInterpolation {
init(stringInterpolation: StringInterpolation) {
self.init(value: stringInterpolation.string)
struct StringInterpolation: StringInterpolationProtocol {
var string = ""
init(literalCapacity: Int, interpolationCount: Int) {
mutating func appendLiteral(_ literal: String) {
mutating func appendInterpolation<T>(_ value: T, privacy: Privacy = .private) where T: CustomStringConvertible {
switch privacy {
case .public:
case .private:
Array(repeating: "*", count: value.description.count).joined()
func log(_ message: Message) {
log("Hello, \("world!", privacy: .public)")
log("Hello, \("secret", privacy: .private)")
log("Static strings say whatever they want")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment