Skip to content

Instantly share code, notes, and snippets.

@alexanderedge
Last active August 29, 2015 14:26
Show Gist options
  • Save alexanderedge/845c0f3bd5aaf2e035c2 to your computer and use it in GitHub Desktop.
Save alexanderedge/845c0f3bd5aaf2e035c2 to your computer and use it in GitHub Desktop.
Swift protocol extensions
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)
}
}
@alexanderedge
Copy link
Author

Compiler error around overloading the pow function.

I'm attempting to give any number the ability to raised to an arbitrary power (specifically, 2).

Do I have to cast to Double in order to use pow?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment