Skip to content

Thoughts on Math.sum/Math.product and implications for this proposal #23

@bakkot

Description

@bakkot

I'm thinking about adding two new methods for doing sum/product. They'd take iterables of numbers: Math.sum([1, 2, 3]) === 6.

And of course if you pass Math.sum an empty array, you'd get 0 out. But then these methods can't really work with BigInts at all: you can't distinguish "an empty array of Numbers" from "an empty array of BigInts", so you can't tell whether 0 or 0n is the identity. And you can't reasonably have Math.sum([1n]) give 1n but also have Math.sum([]) give 0.

So I'm thinking there should be separate Math.sum / Math.bigSum methods, each of which only accepts values of one specific type.

If committee agrees with that analysis, maybe that informs the design of this proposal? Everything currently in this proposal can get away with polymorphism, but that's not going to be true for every possible method, so maybe it will make more sense to split methods into Number/BigInt versions even when we could theoretically combine them. (see also #14)

Alternatively, I suppose, we could have Number.sum and BigInt.sum. That would be pretty weird but it would at least avoid this tension.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions