Skip to content

Instantly share code, notes, and snippets.

@dpgao
Last active March 19, 2018 22:00
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 dpgao/799521f251119ce8bbdf832a1ca1dbb8 to your computer and use it in GitHub Desktop.
Save dpgao/799521f251119ce8bbdf832a1ca1dbb8 to your computer and use it in GitHub Desktop.
infix operator !=> { associativity left precedence 125 }
func !=><T>(lhs: T?, @noescape rhs: () -> Void) -> T? {
if let lhs = lhs { return lhs }
rhs()
return nil
}
let string: String? = "test"
let nilString: String? = nil
if let str1 = string !=> { print("String 1 is nil") },
let str2 = nilString !=> { print("String 2 is nil") } {
print("All optionals are unwrapped")
}
// Output: String 2 is nil
func !=><T: BooleanType>(lhs: T, @noescape rhs: () -> Void) -> Bool {
if lhs { return true }
rhs()
return false
}
func !=><T>(lhs: T?, @autoclosure rhs: () -> Void) -> T? {
return lhs !=> { rhs() }
}
func !=><T: BooleanType>(lhs: T, @autoclosure rhs: () -> Void) -> Bool {
return lhs !=> { rhs() }
}
if let httpResponse = response as? NSHTTPURLResponse
!=> failure(Reason.Other(error), data)
where httpResponse.statusCode == 200
!=> print("Error: \(httpResponse.statusCode)")
!=> failure(Reason.NoSuccessStatusCode(statusCode: httpResponse.statusCode), data),
let responseData = data
!=> failure(Reason.NoData, data),
let result = resource.parse(responseData)
!=> failure(Reason.CouldNotParseJSON, data) {
completion(result)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment