IntStream.sum

Michael Nascimento misterm at gmail.com
Wed Jan 16 04:04:21 PST 2013


It is worth adding to javadoc the intended pattern if users expect an int is:

int sum = Math.toIntExact(intStream.sum());

Regards,
Michael

On Wed, Jan 16, 2013 at 9:58 AM, Remi Forax <forax at univ-mlv.fr> wrote:
> IntStream.sum have just been changed to return a long instead of an int and
> I don't think it's a good idea.
>
> There is 3 ways to deal with overflows:
>   1) do nothing, the Java default semantics
>   2) throw an exception if overflown (using Integer.addExact by example)
>   3) use BigInteger
> all other ways are just half baked solution.
>
> This one is very bad because usually users will use it that way,
>   int sum = (int)intStream.sum();
> so it doesn't solve anything, just make the semantics awkward.
>
> sum() is just a shortcut for a reduce, so it should stick with the default
> Java semantics,
> if users want longs, they can use reduce with Long::add or Long::addExact.
>
> Rémi
>
>
>
>
>


More information about the lambda-libs-spec-observers mailing list