Created
April 21, 2015 23:55
-
-
Save norio-nomura/fa9830bcb269049a3f29 to your computer and use it in GitHub Desktop.
#swift StringInterpolationConvertibleの挙動を確認
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
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