-
-
Save spnkr/b7da6dcd61cf4aa717f4bfa34d3ac8b1 to your computer and use it in GitHub Desktop.
Swift Unless/When
This file contains 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
// Basically, these are if and else statements respectively | |
// without the opposite clause | |
func when(_ test: @autoclosure () -> Bool, action: () -> ()) { | |
if test() { action() } | |
} | |
func unless(_ test: @autoclosure () -> Bool, action: () -> ()) { | |
if !test() { action() } | |
} | |
// Examples | |
when(3 == 5){ | |
println("three equals five") // Will print! | |
} | |
unless(3 == 5){ | |
println("three does not equal five") // Will never print. | |
} | |
when(1 == 1){ | |
println("one equals one") // Will print! | |
} | |
unless(1 == 1){ | |
println("one does not equal one") // Will never print. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment