Skip to content

Instantly share code, notes, and snippets.

@ahcode0919
Last active April 17, 2017 22:54
Show Gist options
  • Save ahcode0919/e1246930fb870783458d91f2583b6205 to your computer and use it in GitHub Desktop.
Save ahcode0919/e1246930fb870783458d91f2583b6205 to your computer and use it in GitHub Desktop.
Creating custom operators in Swift
//Creating an operator
//prefix, postfix, infix
//https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html
//https://developer.apple.com/reference/swift/swift_standard_library_operators
postfix operator ***
///Cubes value
postfix func *** (_ input: Int) -> Int {
return input * input * input
}
2*** //8
infix operator >>>: MultiplicationPrecedence
/// Adds values and multiplies by 3
func >>>(_ lhs: Int, _ rhs: Int) -> Int {
return (lhs + rhs) * 3
}
2>>>3 //15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment