Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
An (unsuccessful) attempt at creating an operator that runs any closure depending on a boolean argument.
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