Skip to content

Singleton emptyList is problematic #2003

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

Closed
VirtuaBoza opened this issue Jul 19, 2024 · 4 comments · Fixed by #2004
Closed

Singleton emptyList is problematic #2003

VirtuaBoza opened this issue Jul 19, 2024 · 4 comments · Fixed by #2004
Labels
awaiting-response Awaiting response from creator. Can't fix without it. with-mutations

Comments

@VirtuaBoza
Copy link

I just ran down a nasty bug related to this code below:

return EMPTY_LIST || (EMPTY_LIST = makeList(0, 0, SHIFT));

I did not realize that every time I was creating a new list with List(), I was getting the same instance of List. This seems counterintuitive in a library based on the idea of immutability.

Some code in the project was mutating an instance of an empty list which caused all subsequent newly minted lists to be non-empty. Obviously the code in my project was incorrect, but I came here to suggest not using a singleton pattern here regardless.

@jdeniau
Copy link
Member

jdeniau commented Jul 19, 2024

Nice catch. A possible fix would be either to make the new list not mutable, or create a new list every time, or reset EMPTY_LIST if it has been mutated.

@bdurrer
Copy link
Contributor

bdurrer commented Jul 22, 2024

That's funny...In our project we made our own EMPTY_LIST to allow skipping React's "did something change" checks when consecutive calls returned nothing.

I am a bit suprised that we seem to just have done that as a reaction to reading the code, because a test would obviously have caught that

@jdeniau
Copy link
Member

jdeniau commented Jul 22, 2024

@VirtuaBoza Can you share with us a reproductible example ?

I tried to implement #2004 but I am not able to reproduce your issue. I tested with asMutable and withMutation without success.

@jdeniau jdeniau added with-mutations awaiting-response Awaiting response from creator. Can't fix without it. labels Jul 22, 2024
@VirtuaBoza
Copy link
Author

@VirtuaBoza Can you share with us a reproductible example ?

I tried to implement #2004 but I am not able to reproduce your issue. I tested with asMutable and withMutation without success.

The problematic code was as follows:

Object.assign(List(), newIds)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-response Awaiting response from creator. Can't fix without it. with-mutations
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants