Created
February 3, 2017 02:37
-
-
Save ukitaka/ec92baedecff095358c4c5a2475c1ba5 to your computer and use it in GitHub Desktop.
ExpressibleByStringLiteral
This file contains hidden or 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
struct KeyPath { | |
let key: String | |
init(_ key: String) { | |
self.key = key | |
} | |
} | |
extension KeyPath: ExpressibleByStringLiteral { | |
init(unicodeScalarLiteral value: String) { | |
self.key = value | |
} | |
init(extendedGraphemeClusterLiteral value: String) { | |
self.key = value | |
} | |
init(stringLiteral value: String) { | |
self.key = value | |
} | |
} | |
func findBy(keyPath: KeyPath) { | |
print("OK") | |
} | |
findBy(keyPath: KeyPath("Key")) //OK | |
findBy(keyPath: "StringLiteral") //OK | |
let strVar: String = "strVar" | |
findBy(keyPath: strVar) //NG |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ExpressibleByStringLiteral なのでStringLiteralじゃないとだめという例