Last active
August 29, 2015 14:26
-
-
Save alexanderedge/845c0f3bd5aaf2e035c2 to your computer and use it in GitHub Desktop.
Swift protocol extensions
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 Raisable { | |
| func raise(exponent : Self) -> Self | |
| } | |
| extension Raisable where Self : SignedNumberType { | |
| func raise(exponent : Self) { | |
| return pow(self, exponent) | |
| } | |
| } | |
| protocol Squarable : Raisable { | |
| func squared() -> Self | |
| } | |
| extension Squarable { | |
| func squared() -> Self { | |
| return self.raise(2) | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Compiler error around overloading the
powfunction.I'm attempting to give any number the ability to raised to an arbitrary power (specifically, 2).
Do I have to cast to
Doublein order to usepow?