Skip to content

Instantly share code, notes, and snippets.

@apparentsoft
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save apparentsoft/95707a39f00f444ac9b8 to your computer and use it in GitHub Desktop.

Select an option

Save apparentsoft/95707a39f00f444ac9b8 to your computer and use it in GitHub Desktop.
An operator similar to how I'd image ?: to work. Check option and provide a specified (right-hand-side) value if it's nil.
operator infix ||| {associativity left precedence 100}
func ||| <T>(lhs:T?, rhs: @auto_closure () -> T) -> T {
if let alhs = lhs {
return alhs
} else {
return rhs()
}
}
let opt1:Int? = 12
let a = opt1 ||| 3
// a is 12
let opt2:Int? = nil
let opt3:Int? = nil
let b = opt2 ||| opt3 ||| 5
// b is 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment