Last active
April 4, 2017 01:29
-
-
Save manuelCarlos/97e39d6aecb89d60083ce919561428b0 to your computer and use it in GitHub Desktop.
An (unsuccessful) attempt at creating an operator that runs any closure depending on a boolean argument.
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
infix operator <-? | |
func <-? <T> (defaultValue: @autoclosure () throws -> T , condition: Bool ) rethrows -> T { | |
if condition{ | |
return try defaultValue() | |
} | |
return () as! T | |
} | |
var v: Int? = 100 | |
let closure = { return 3 } | |
v = closure() <-? true | |
print("closure:", v ?? 0) | |
print("hello") <-? true | |
print("hello") <-? false // works ok | |
v = 100 <-? true | |
print("Assigment:", v ?? 0) | |
//v = 100 <-? false // CRASH ! | |
// was hoping for a way to not return anything here, not even nil 😅 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment