Skip to content

Instantly share code, notes, and snippets.

@lilyball
Last active August 29, 2015 14:04
Show Gist options
  • Save lilyball/5fa08916c1f480bae38d to your computer and use it in GitHub Desktop.
Save lilyball/5fa08916c1f480bae38d to your computer and use it in GitHub Desktop.
`x !|| y` operator to provide the equivalent of `x ? x! : y`
operator infix !|| {
associativity right
precedence 100
}
func !||<T>(left: T?, right: @auto_closure () -> T) -> T {
if let val = left {
return val
}
return right()
}
@lilyball
Copy link
Author

let x = foo() !|| "default value"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment