Skip to content

Instantly share code, notes, and snippets.

@manuelCarlos
Last active April 4, 2017 01:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manuelCarlos/97e39d6aecb89d60083ce919561428b0 to your computer and use it in GitHub Desktop.
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.
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