Last active
October 12, 2017 16:51
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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