Skip to content

Instantly share code, notes, and snippets.

@jckarter
Created May 16, 2015 20:13
Show Gist options
  • Save jckarter/019f317b669961add5a3 to your computer and use it in GitHub Desktop.
Save jckarter/019f317b669961add5a3 to your computer and use it in GitHub Desktop.
matching tuple value
protocol Consable {
typealias Tuple
}
struct HCons<T, U: Consable>: Consable {
typealias Tuple = (T, U.Tuple)
}
struct HNil: Consable {
typealias Tuple = ()
}
func ~=<T: Consable>(a: T.Tuple, b: T) -> Bool {
return true
}
func id<T>(x: T) -> T { return x }
switch (HCons<Int, HCons<Float, HNil>>()) {
// Need 'id' to trick compiler into thinking this is an expr pattern, not a tuple pattern
case id(1, (2.0, ())):
println("ok")
default: fatalError()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment