-
-
Save onmyway133/0f0c906b9ea33865c745ebfa27b6decd 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
be aware https://airspeedvelocity.net/2014/06/28/extending-the-swift-language-is-cool-but-be-careful/