Swift: generic power function as infix operator -
below answer original question of how add power operator int numbers:
infix operator ^^ { associativity left precedence 160 } func ^^ (radix: int, power: int) -> int { return int(pow(cgfloat(radix), cgfloat(power))) } how 1 generalise works double, float, , int?
you cannot "generalize" it. have implement each type separately.
if in swift header, see that swift built-in operators such +. there no "general" + function; there 1 + function every numeric type.
thus:
infix operator ^^ { associativity left precedence 160 } func ^^ (radix: int, power: int) -> int { return int(pow(cgfloat(radix), cgfloat(power))) } func ^^ (radix: double, power: double) -> double { return double(pow(cgfloat(radix), cgfloat(power))) } // ... , on many types ...
Comments
Post a Comment