// I'm trying to figure out how to use Generics work with Operators
// I have 1 goal, to use generics with math operators, but 3 different ways to caompilsh it, depending on the quesion answers
// 1. Ideally there would be a protocol known as computable which let me work with generic types so that I could write
func square<T: ArithmaticComputabale>(a: T) { return a * a }
// this should work for anything that conforms to ArithmaticComputabale,
// But I could find no such protocol so next I looked for
// 2. How to restrict generics to being of certain types. I'd like to be able to write this
func square<T: (Float, Int, Double)>(a: T) { return a * a }
// the purpose being that now I can use square with these types.
// But I couldn't find the syntax for that, so I tried creating my own counting one the identical signatures for multiplycation
// 3. How to accomplish the same thing given that the swift library defines these multiplication variants
infix operator * { associativity left precedence 150 }
public func *(lhs: Float, rhs: Float) -> Float
public func *<T : _IntegerArithmeticType>(lhs: T, rhs: T) -> T
public func *(lhs: Double, rhs: Double) -> Double
// I'd like to be able to do something like this
public protocol Multiplyable {
typealias T
public func *(lhs: T, rhs: T) -> T
}
extension Float: Multiplyable { // should conform already given the swift library
}
extension Double: Multiplyable {
}
extension Int: Multiplyable {
}
// Then finally I'd be able to do this
func square<T: Multiplyable>(a:T) -> T {
return a * a
}
// But everything I've tried there has errored. Does anyone know how to accomplish any or all of these 3?
// Note, not having seen computable, ahd having seen the 3 essentially identical *operator signatures, I'm not sure how these Generics are
// useful for any type where you aren't clear of the conformance rules.