Skip to content
Playground(another site)

bynk.query.sum_needs_numeric

Error A sum/average key function does not return a numeric type (Int, Float, or Duration; sum also takes a Decimal, average does not).

sum and average add the values the key function returns, so the key must be numeric: Int, Float or Duration, or a refined type over one of them. A String or a record cannot be added. Return a numeric field. sum also adds a Decimal key, exactly, on a List and on a Query. average does not take one: a mean is a division, and a Decimal division needs a scale and a rounding mode, so sum the key, then div(Decimal.fromInt(n), scale, mode) by the count. To pick by an ordering rather than add, min, max and sortBy accept any orderable key.

Example

type Line = { sku: String, qty: Int }
fn total(lines: List[Line]) -> Int {
let t = lines.sum((l) => l.sku) // ✗ a `String` cannot be summed
0
}
fn total(lines: List[Line]) -> Int {
lines.sum((l) => l.qty) // ✓ a numeric key
}

In the Book

See also

  • Every code in its category, Other, on the diagnostic index.
  • bynk explain bynk.query.sum_needs_numeric shows the same from the installed toolchain, without a network.