Skip to content

Instantly share code, notes, and snippets.

@ole
Last active August 7, 2018 10:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ole/a369f6a145235a28b5c4d6e4b1ac8dba to your computer and use it in GitHub Desktop.
Save ole/a369f6a145235a28b5c4d6e4b1ac8dba to your computer and use it in GitHub Desktop.
/// Ignores `value` and just evaluates `pattern`.
/// Can be used to evaluate arbitrary expressions in a switch.
func ~=<A>(pattern: @autoclosure () -> Bool, value: A) -> Bool {
return pattern()
}
let (x, y, z) = (0, 0, 1)
switch () /* can be anything */ {
case x > 0: print("x matches")
case y < 0: print("y matches")
case z == 1: print("z matches")
default: print("no match")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment