Skip to content

Instantly share code, notes, and snippets.

@trilliwon
Created December 13, 2016 07:13
Show Gist options
  • Save trilliwon/3fe5db4c1e0c6ec47d822e635d6e8947 to your computer and use it in GitHub Desktop.
Save trilliwon/3fe5db4c1e0c6ec47d822e635d6e8947 to your computer and use it in GitHub Desktop.
custom operators in swift
infix operator ???
infix operator ?->
// user.name ??? sendMessage()
// is euqual to
// if let name = user.name {
// sendMessage()
// }
public func ???<T>(optional: T?, defaultClosure: @autoclosure () -> () ){
if optional != nil {
defaultClosure()
}
}
// isAuthenticatedUser ?-> sendMessage()
// is euqal to
// if isAuthenticatedUser {
// sendMessage()
// }
public func ?-> (lhs: Bool, rhs: @autoclosure () -> ()) {
if lhs { rhs() }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment