Skip to content

Instantly share code, notes, and snippets.

@anettodev
Created April 8, 2019 01:53
Show Gist options
  • Save anettodev/ee0ecb91853bc0c36195f0f41d84d143 to your computer and use it in GitHub Desktop.
Save anettodev/ee0ecb91853bc0c36195f0f41d84d143 to your computer and use it in GitHub Desktop.
// Custom Protocol
protocol CustomAddable {
static func +(lhs: Self, rhs: Self) -> Self
}
// Sum 2 Numeric elements
func sum<T: CustomAddable>(x: T, y: T) -> T {
return x + y
}
// extending to work with the Custom Protocol
extension Int: CustomAddable {}
extension Double: CustomAddable {}
extension Float: CustomAddable {}
sum(3, 4.5) // result 7.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment