-
Notifications
You must be signed in to change notification settings - Fork 205
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
Records: What are the semantics of identity? #2390
Comments
I'm 100% OK with that if the rest of the language team is. |
If we choose the second formulation it is easier to change our mind later rather than vice versa. |
I also prefer the approach where the compiler is allowed to implement identity as pointer identity. |
In our last language team meeting I felt like I was the last holdout who wasn't ok with this change, but y'all convinced me. |
I think it's fine too. We should be aware that it probably means that some programming patterns will consistently give identical records, and some will not, and people may start depending on the concrete implementation, to the point where we can't change it anyway because it breaks some semi-large number of tests somewhere. Would be we be willing to add a randomness-factor in debug mode? Say, identity of structs becomes pointer identity |
Whatever we do here, we should probably do precisely the same for data-classes — with some suitable definition of "same shape", which for data-classes is likely the same struct declaration as runtime type, and "same type arguments" |
One thing to consider: Can identical return Consider a program where the record shape The someone asks whether So, if the Do we want to prevent that? Or would it be fine to say |
I think if we don't want to use structural equality for identity, then we will make The 2nd option suggested above makes Even better option would be to always return |
We may or may not need to specify The For records, it's practically useless. The one requirement of an identity hash code is that for identical values it should return the same value. For records, which do not have a consistent identity over time, generating a new random value for each call is compatible with Which means we don't have to care about the implementation of |
I would be against this as it required testing that an operand is a record for very little benefit. If we can't trace the record to know exactly what operations happen on a record, then we must assume dart2js has an optimization that erases unused fields, but it would be confused by the presence of |
Good point that (For web compilation, we should totally make integers structs with no equality, so it's no longer wrong to have |
* [patterns] Revise record identical() to be faster. Fix #2390. * Revise non-normative text.
The records proposal specifies identity here to be structural. In particular, identity is required to return
true
for any two records with the same shape for which the fields are pointwise identical.We have discussed an alternative specification which would look more like the following:
In either of the two formulations compilers required neither to preserve pointer identity, nor to preserve pointer dis-identity. That is, a compiler may unbox and re-allocate a record freely, or it may replace two source level records with identical components with a single allocation.
However, in the second formulation, compilers are free to implement identity as pointer identity, rather than requiring a slow recursive traversal. This also has implications even for non-record based code, since all identity checks for which the compiler cannot prove that a record reaches neither argument must now include conditional logic to handle the case of records.
Should we change this specification?
cc @munificent @eernstg @lrhn @jakemac53 @natebosch @stereotype441 @rakudrama @mraleph @alexmarkov
The text was updated successfully, but these errors were encountered: