Last active
February 22, 2019 07:53
-
-
Save chriseidhof/95e59be97a5b39ac4bed3460cc84cf4d to your computer and use it in GitHub Desktop.
String Interpolation Experiments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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