Skip to content

Instantly share code, notes, and snippets.

@norio-nomura
Created April 21, 2015 23:55
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 norio-nomura/fa9830bcb269049a3f29 to your computer and use it in GitHub Desktop.
Save norio-nomura/fa9830bcb269049a3f29 to your computer and use it in GitHub Desktop.
#swift StringInterpolationConvertibleの挙動を確認
import Foundation
class C {}
// support own class without Printable
extension String {
init(stringInterpolationSegment expr: C) {
self.init(format: "C(%d)", ObjectIdentifier(expr).hashValue)
}
}
let c = C()
String(stringInterpolationSegment: c) // "C(1137743216)
"\(c)" // "C(1137743216)
// Create own type support StringInterpolationConvertible
struct Interpolation: StringInterpolationConvertible {
var identifiers = [String]()
init<T>(stringInterpolationSegment expr: T) {
println("generic") // (2 times)
identifiers = [String(stringInterpolationSegment: expr)]
}
init(stringInterpolation strings: Interpolation...) {
println("non generic")
identifiers = strings.flatMap {
$0.identifiers // (2 times)
}
}
}
let i: Interpolation = "test\(C())\(1+1)" // {["test","__lldb_expr_511.C", "2"]}
i.identifiers // ["test","__lldb_expr_511.C", "2"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment