-
Notifications
You must be signed in to change notification settings - Fork 795
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
Seq.init
does not save values as suggested in description
#14233
Comments
Btw, in contrast, note this for
The F# Core code for |
Hmm... I agree the description is a bit misleading given the actual behavior, and I am not sure what actually was intended here. But I want to point out that there is |
=> I would suggest to keep the code (backwards compat), change the doc function and possibly mention within that doc comment to use Seq.cache to prevent recreating the members. |
Yeah, we won't be able to change the behaviour. |
Exactly, that was why I brought it up:
I’ll tinker over a proposal for a better description. |
The description and tooltip of
Seq.init
are as follows (emphasis mine):This suggests that for any sequence, the initializer is called only once and that successive access would use the saved value. I was inspecting the code and couldn't find how this was done and it turns out, it isn't. Not really, at least.
Repro steps
Basically, just this:
Expected behavior
If the values from the generator were indeed saved, the
initializer
function would not be called again. However, theSeq.last
returns different values, which means the mutable int I used gets updated again.Actual behavior
The values aren't saved. At least not in the way I'd expect. They are saved momentarily for a call to
Current
, in such cases where you would use theIEnumerator<_>.Current
directly. This saving itself is done in aLazy<_>
type to prevent problems if multiple threads read the value.Known workarounds
Just ensure you don't write an expensive function as generator.
Related information
It's very well possible that this is "expected", and it is likely being used with side effects in practice. I'm bringing this up to discuss a better description for this function, as the way it is currently written, it confused me and possibly others.
The text was updated successfully, but these errors were encountered: