Skip to content

Instantly share code, notes, and snippets.

@chriseidhof
Last active February 22, 2019 07:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chriseidhof/95e59be97a5b39ac4bed3460cc84cf4d to your computer and use it in GitHub Desktop.
Save chriseidhof/95e59be97a5b39ac4bed3460cc84cf4d to your computer and use it in GitHub Desktop.
String Interpolation Experiments
//
// main.swift
// StringLiterals
//
// Created by Chris Eidhof on 21.02.19.
// Copyright © 2019 objc.io. All rights reserved.
//
import Foundation
protocol SQLValue {}
extension Int: SQLValue {}
extension String: SQLValue {}
struct Query {
var query: String
var values: [SQLValue] = []
}
extension Query: ExpressibleByStringLiteral {
init(stringLiteral value: String) {
self.query = value
}
}
extension Query: ExpressibleByStringInterpolation {
struct StringInterpolation: StringInterpolationProtocol {
var query: String
var values: [SQLValue] = []
init(literalCapacity: Int, interpolationCount: Int) {
print("\(literalCapacity) \(interpolationCount)")
self.query = ""
self.values = []
}
mutating func appendLiteral(_ literal: String) {
query.append(literal)
}
mutating func appendInterpolation<V: SQLValue>(_ x: V) {
query.append("$\(values.count)")
values.append(x)
}
}
init(stringInterpolation i: StringInterpolation) {
self.query = i.query
self.values = i.values
}
}
let x: Query = "SELECT * from users where name == \("chris") AND age > \(20)"
print(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment