Skip to content

Instantly share code, notes, and snippets.

@ole
Last active October 12, 2017 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ole/73f26723bf68d7443ec415b5a11eeb1b to your computer and use it in GitHub Desktop.
Save ole/73f26723bf68d7443ec415b5a11eeb1b to your computer and use it in GitHub Desktop.
Pattern matching for types. See https://twitter.com/Gernot/status/918490645118976000 for context.
func ~= <T, U> (pattern: T.Type, value: U.Type) -> Bool {
return pattern == value
}
func f<T>(_ type: T.Type) {
switch T.self {
case Int.self:
print("Int")
default:
print("Something else")
}
}
f(Int.self)
f(String.self)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment