klyn.Application.sum
method
public static sum<T extends AbstractNumber>(notnull values as Tuple) as T
Description

Sums a tuple of primitive numbers and complex numbers.

The compiler infers T from all positional types using numeric promotion. An empty tuple returns Int(0). Non-numeric positions and signed/ULong combinations without a common numeric type are rejected with TypeError. An explicit T may widen, but never narrow, the inferred numeric type. A tuple whose positional types have been erased requires an explicit T; its elements are then validated at runtime. No temporary list is created.

Parameters
ParameterDescription
valuesNumeric tuple to sum.
Returns

Sum in the inferred or explicitly widened numeric type T.

Throws
  • TypeError if the tuple contains a non-numeric value.
  • NullException if values or a numeric element is null at runtime. A statically null argument or a literal null positional type is rejected with TypeError.
Example
import klyn.math
total = sum((10, 30.1, 3 + 2j))
assert total is Complex<Double>
assert total == 43.1 + 2j
assert sum((10, 20.5)) == 30.5