You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I just spent about half an hour trying to figure out why data was showing up in places it shouldn't. It turned out that my defaulted values were being passed by reference, meaning that any modifications to one instance would affect all current and future instances created from the same struct.
// BAD: array is passed by referenceconstMyType=defaulted(array(string()),[]);constinstanceA=create(undefined,MyType);instanceA.push("hello");constinstanceB=create(undefined,MyType);console.log(instanceB);// [ "hello" ]
While this makes sense in hindsight, it was pretty painful to debug (especially since my case had a significantly more complex data structure).
As such, I think this project would be improved if this pitfall was documented. I've created a PR that adds this documentation.
Additionally, I noticed that the type signatures for defaulted allow any as the default value. Is this intentional? I imagine that stricter types would allow for significantly improved safety, helping users to avoid this issue (eg only accepting a plain value if the value is a type that cannot be modified).
The text was updated successfully, but these errors were encountered:
Ok looks like the issue is more complex than I thought, I'll close this issue and the related PR and open a new issue that describes the problem better.
I just spent about half an hour trying to figure out why data was showing up in places it shouldn't. It turned out that my
defaulted
values were being passed by reference, meaning that any modifications to one instance would affect all current and future instancescreate
d from the same struct.While this makes sense in hindsight, it was pretty painful to debug (especially since my case had a significantly more complex data structure).
As such, I think this project would be improved if this pitfall was documented. I've created a PR that adds this documentation.
Additionally, I noticed that the type signatures for defaulted allow
any
as the default value. Is this intentional? I imagine that stricter types would allow for significantly improved safety, helping users to avoid this issue (eg only accepting a plain value if the value is a type that cannot be modified).The text was updated successfully, but these errors were encountered: