-
-
Notifications
You must be signed in to change notification settings - Fork 263
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
Using flow
in combination with Effect.reduce
incorrectly reuses past accumulator
#4268
Comments
The issue is that you are mutating the same variable on every run. If you want a new instance each time then you need to wrap the creation with something like Effect.sync first. The "good" versions works because it is wrapped with a closure. |
Thanks for getting back to me so quickly @tim-smart . I agree that somewhere along the line a reference to the object created at Every time the |
If you are supplying the same reference as the initial value every time then it is working as intended. If you want to work with mutable state you have to be wary of the foot-guns. In this case you have to ensure a new instance of the number class is created on each run of the effect - that is something the developer is responsible for :) We have been evaluating the use of lazy evaluation for the initial argument to help mitigate this, but for now it is up to you. |
Thanks for stepping through it all with me, after reading your replies I understand the issue. A potential solution would be this
Perhaps a function like this could be a nice little bit of 'sugar'
(As an aside I cant see what the difference between |
What version of Effect is running?
3.12.1
What steps can reproduce the bug?
This test demonstrates the bug clearly
What is the expected behavior?
I would expect this test to output
What do you see instead?
Instead I see an incorrect result for sumUpNumbersBad result 2
Additional information
Potential issues with
flow
are hinted at in the docs herehttps://effect.website/docs/code-style/guidelines/#avoid-tacit-usage
But I still believe that this behavior is unacceptable and could impact many users of Effect.
The text was updated successfully, but these errors were encountered: