Last active
June 17, 2019 08:36
-
-
Save AppleCEO/833f1c88766555c95f37e1d29c5ee2d6 to your computer and use it in GitHub Desktop.
Generic and Custom Operators Swift Example
This file contains hidden or 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
| protocol Multipleiable { | |
| static func * (lhs: Self, rhs: Self) -> Self | |
| } | |
| extension Int: Multipleiable {} | |
| extension Double: Multipleiable {} | |
| infix operator ** : MultiplicationPrecedence | |
| func **<T> (lhs: T, rhs: T) -> T where T: Multipleiable { | |
| return lhs * rhs | |
| } | |
| 3 ** 4 | |
| // result : 12 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment