Created
April 9, 2015 22:25
-
-
Save danielpi/43caf4f39c0322cf193b to your computer and use it in GitHub Desktop.
A protocol to use when you want to write a generic function that can accept Ints, Floats or Doubles.
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 MathematicsProtocol: Equatable, Comparable, IntegerLiteralConvertible { | |
init(_ value: Int) | |
init(_ value: Float) | |
init(_ value: Double) | |
func +(lhs: Self, rhs: Self) -> Self | |
func -(lhs: Self, rhs: Self) -> Self | |
func * (lhs: Self, rhs: Self) -> Self | |
func / (lhs: Self, rhs: Self) -> Self | |
} | |
extension Int: MathematicsProtocol {} | |
extension Float: MathematicsProtocol {} | |
extension Double: MathematicsProtocol {} | |
// Equatable means you can use == | |
// Comparable means you can use < > <= >= | |
// IntegerLiteralConvertible means you can use integer literals | |
// Credit to | |
// http://stackoverflow.com/questions/27321763/return-int-from-a-generic-mathematics-type-in-swift | |
// http://stackoverflow.com/questions/25592838/generics-of-raw-types-int-float-double-create-weird-error-messages |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment