Skip to content

Instantly share code, notes, and snippets.

@ttepasse
Last active August 29, 2015 14:11
Show Gist options
  • Save ttepasse/bbb47311ca8a5e6f0263 to your computer and use it in GitHub Desktop.
Save ttepasse/bbb47311ca8a5e6f0263 to your computer and use it in GitHub Desktop.
class Bookmark {}
class WebView {}
class Something {}
var bookmark : Bookmark? = Bookmark()
var webview : WebView? = WebView()
var something : Something? = nil
infix operator &&! {}
func &&! <LeftType, RightType> (lhs: LeftType?, rhs: RightType?) -> (LeftType, RightType)? {
if let left: LeftType = lhs {
if let right: RightType = rhs {
return (left, right)
}
}
return nil
}
if let (b, w) = bookmark &&! webview {
println(true)
}
// -> true
if let (b, s) = bookmark &&! something {
println(true)
} else {
false
}
// -> false
infix operator &&? { associativity left }
func &&? <LeftType, RightType> (lhs: LeftType?, rhs: RightType?) -> Bool {
if let left: LeftType = lhs {
if let right: RightType = rhs {
return true
}
}
return false
}
func &&? <FirstType> (lhs: FirstType?, rhs: @autoclosure () -> Bool) -> Bool {
if let left: FirstType = lhs {
return rhs()
}
return false
}
bookmark &&? webview // -> true
bookmark &&? something // -> false
bookmark &&? webview &&? webview // -> true
bookmark &&? webview &&? something // -> false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment