Last active
April 17, 2017 22:54
-
-
Save ahcode0919/e1246930fb870783458d91f2583b6205 to your computer and use it in GitHub Desktop.
Creating custom operators in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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