Skip to content

Instantly share code, notes, and snippets.

@rayfix
Created June 22, 2017 06:57
Show Gist options
  • Save rayfix/c887200c45ec29dd34f6a3397e7ce0a6 to your computer and use it in GitHub Desktop.
Save rayfix/c887200c45ec29dd34f6a3397e7ce0a6 to your computer and use it in GitHub Desktop.
struct BeginsWith {
let value: String
init(_ value: String) {
self.value = value
}
}
func ~=(pattern: BeginsWith, value: String) -> Bool {
return value.hasPrefix(pattern.value)
}
let s = "Hello world"
switch s {
case BeginsWith("Hello"):
print("got it \(s)")
case BeginsWith("Goodbye"):
print("didn't get it \(s)")
default:
break
}
////////////////////////////////////////////////////
func ~=(pattern: String, value: Int) -> Bool {
return pattern == String(describing: value)
}
let x = (1,2,3)
switch x {
case ("10", "2", _):
print("Got it")
default:
print("Didn't get it")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment