Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inner class <name>SumProcedure in abstractLazyPrimitiveIterable.stg should widen sum type for int, short, etc. #1452

Open
donraab opened this issue Apr 11, 2023 · 2 comments
Assignees

Comments

@donraab
Copy link
Contributor

donraab commented Apr 11, 2023

Today, the generated code for lazy int returns the following, which will overflow silently for large sums.

    private static final class IntSumProcedure implements IntProcedure
    {
        private int sum = 0;

        @Override
        public void value(int each)
        {
            this.sum += each;
        }

        public int getValue()
        {
            return this.sum;
        }
    }

https://github.com/eclipse/eclipse-collections/blob/master/eclipse-collections-code-generator/src/main/resources/impl/lazy/abstractLazyPrimitiveIterable.stg#L451

This was a Project Loom code example in a test that I wrote that failed (using Java 20):

    @Test
    public void usingLoomWithEclipseCollectionsLazyAsParallel()
    {
        long sum = Interval.oneTo(2_000_000)
                .toList()
                .asParallel(Executors.newVirtualThreadPerTaskExecutor(), 2)
                .sumOfInt(each -> each * 2);

        long expected = IntInterval.oneTo(2_000_000).asLazy().collectInt(each -> each * 2).sum();
        Assertions.assertEquals(expected, sum);
    }

Expected :1387447424
Actual :4000002000000

A similar test could be written in Java 8 without using asParallel.

@emilie-robichaud
Copy link
Contributor

Hi, can you assign this issue to me?

@donraab
Copy link
Contributor Author

donraab commented Apr 11, 2023

Thanks for volunteering @emilie-robichaud ! I have assigned the issue to you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants