Created
July 7, 2020 19:49
-
-
Save 0xLeif/00795075c39180ca4cdbe63c9d1655f4 to your computer and use it in GitHub Desktop.
One Line Switch
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 switched<T: Hashable, O>(_ value: T, | |
cases: [T: O?], | |
default defaultCase: O? = nil) -> O? { | |
guard let switchCase = cases[value] else { | |
return defaultCase | |
} | |
return switchCase | |
} | |
func switched<T: Hashable, O>(_ value: T, | |
cases: [T: () -> O?], | |
default defaultCase: () -> O? = { nil }) -> O? { | |
guard let switchCase = cases[value] else { | |
return defaultCase() | |
} | |
return switchCase() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment