Skip to content

Instantly share code, notes, and snippets.

@ukitaka
Created February 3, 2017 02:37
Show Gist options
  • Save ukitaka/ec92baedecff095358c4c5a2475c1ba5 to your computer and use it in GitHub Desktop.
Save ukitaka/ec92baedecff095358c4c5a2475c1ba5 to your computer and use it in GitHub Desktop.
ExpressibleByStringLiteral
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
@ukitaka
Copy link
Author

ukitaka commented Feb 3, 2017

ExpressibleByStringLiteral なのでStringLiteralじゃないとだめという例

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment