Skip to content

Instantly share code, notes, and snippets.

@saiday
Last active December 28, 2019 12:53
Show Gist options
  • Save saiday/119c0ee9b4da1bbfc5da5f9045410030 to your computer and use it in GitHub Desktop.
Save saiday/119c0ee9b4da1bbfc5da5f9045410030 to your computer and use it in GitHub Desktop.
func ~=<T>(rhs: KeyPath<T, Bool>, lhs: T) -> Bool {
lhs[keyPath: rhs]
}
func handle(_ character: Character) {
switch character {
case "<":
print("element")
case "#":
print("hashtag")
case \.isNumber:
print("number")
case \.isNewline:
print("new line")
default:
print("others")
}
}
var a: Character = "3"
handle(a)
if case \.isNumber = a {
print("numberi still matched")
}
a = "\n"
handle(a)
if case \.isNewline = a {
print("new line still matched")
}
a = "#"
handle(a)
if case "#" = a {
print("hashtag still matched, but not matching self-defined pattern `~=<T>(rhs: KeyPath<T, Bool>, lhs: T) -> Bool`")
}
// output:
// number
// numberi still matched
// new line
// new line still matched
// hashtag
// hashtag still matched, but not matching self-defined pattern `~=<T>(rhs: KeyPath<T, Bool>, lhs: T) -> Bool`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment