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
Currently, since struct instances are sealed, there is no built-in mechanism for native deep equality checks.
This limitation often requires developers to rely on helper functions like deepEqual to compare objects based on their data rather than their references.
While this works, certain scenarios make implementing deep equality challenging. For example, in React, dependency arrays ([]) in hooks like useEffect rely on strict reference equality (===). If an object is passed directly without ensuring it retains the same reference, the behavior can become unpredictable:
constperson={name: "a"}useEffect(()=>{console.log(Math.random())// // This runs unexpectedly if `person`'s reference changes},[person])
In cases like these, the lack of a native equality mechanism for struct instances increases cognitive overhead and complicates development.
Proposed Solution
If struct could natively support deep equality checks, it would significantly reduce the mental load for developers, particularly in scenarios where strict reference equality is not practical or desired. For example:
Struct fields are writable. Two separate struct instances can't be === referentially equal because either of them could be modified, so they need to remain separate instances.
consta1=newPerson("a")consta2=newPerson("a")a1===a2;// needs to be falsea1.name="b";a1===a2;// because this must be false
Currently, since struct instances are sealed, there is no built-in mechanism for native deep equality checks.
This limitation often requires developers to rely on helper functions like
deepEqual
to compare objects based on their data rather than their references.While this works, certain scenarios make implementing deep equality challenging. For example, in React, dependency arrays (
[]
) in hooks likeuseEffect
rely on strict reference equality (===
). If an object is passed directly without ensuring it retains the same reference, the behavior can become unpredictable:In cases like these, the lack of a native equality mechanism for struct instances increases cognitive overhead and complicates development.
Proposed Solution
If
struct
could natively support deep equality checks, it would significantly reduce the mental load for developers, particularly in scenarios where strict reference equality is not practical or desired. For example:Benefits
The text was updated successfully, but these errors were encountered: