Closed porky11 closed 7 years ago
how this could be implemented for every function itself or as a macro like these:
(def - (macro extern (i)
(* i (minus-one i))))
(def - (macro extern (i)
(qq - (zero i) i)))
I'd prefer the second macro approach.
(zero i)
just returns a zero-value for type of i.
The *'
operator could also be used:
(import cstdio)
(def main (fn extern-c int (void)
(let ((a int 10)
(b int16 10)
(c float 10.0))
(printf "%d\n" (*' a -1))
(printf "%d\n" (*' b -1))
(printf "%f\n" (*' c -1))
0)))
But this way, no unary minus can be implemented generally, since -1 cannot be cast to some kind of Vector type or similar
*'
is overloaded on the first type, such that extra definitions could be added, but it's true that that would be pretty awkward for a type for which unary minus is defined but subtraction is not. Unary minus is now defined over the standard numeric types.
(- 0.0 x)
also works instead of(- x)
but only works for x as floating point type