-
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
Can records be const? #2337
Comments
I'd say yes. That only matters for whether the record can be used in a |
Yeah, I don't see any reason to disallow const records. I'll leave this open as a reminder to update the proposal to include it. |
This should probably get added in soon. |
@munificent kind reminder to get this into the spec. |
I've written up a proposal for constant record behavior (#2413). I've made some choices in there that might need discussion:
That's basically it. The consequences are: No
|
Certainly! I would expect the implicitly induced operator Hopefully this would allow us to lint against We could treat structs / value classes the same, and get the same effect. The other reason why I'm arguing that we might want to allow records to be explicitly const is
that is: to document the intention and to get a compile-time error if its components aren't actually constant. If we do not ensure any kind of record canonicalization explicitly then I suspect that we'll have it implicitly: How would backends otherwise be able to canonicalize objects with instance variables whose value is a record? class C {
final (int, int) pair;
const C(int i1, int i2): pair = (i1, i2);
}
class D {
final (C, C) pair;
const D(C c1, C c2): pair = (c1, c2);
} If constant expressions yielding an instance of Another thing is that if we do support class E {
final List<int> xs;
final int j;
const E(int i, this.j): xs = const [1, i]; // OK.
}
void main() {
C(42, 43); // OK.
var i = 42;
C(i, 43); // OK.
C(42, i + 1); // Compile-time error.
} This has been requested, e.g., recently in #823 (comment), and it would probably be a quite useful feature if we allow collection literals to be "constant after actual argument substitution".
That would be nice, and it fits nicely into #2219. |
As pointed out elsewhere (last language-team meeting I think), that's not actually a valid optimization. There are objects which are identical, but not Still, it means that
Ouch. That is a good question, and a reason why we should perhaps canonicalize records.
Then we say that we canonicalize objects to the same instance if the values of their instance variables are structurally equivalent. That's something we're willing to check at compile-time, even if we don't do it at runtime. Basically, we introduce the "recursive identical" functionality in the specification, but do not use it for the That's the equivalence we really care about, we just don't want to make We could expose that relation, as a static I would not allow |
* Address a bunch of records issues. - Support constant records. Fix #2337. - Support empty and one-positional-field records. Fix #2386. - Re-add support for positional field getters Fix #2388. - Specify the behavior of `toString()`. Fix #2389. - Disambiguate record types in `on` clauses. Fix #2406. * Clarify the iteration order of fields in `==`. * Copy-edit the sections on const records and canonicalization. There should be no meaningful changes. I just: - Fixed some misspellings. - Used Markdown style consistent with the rest of the doc. - Re-worded things to, I hope, read a little more naturally. - Removed the parenthetical on identical() in a const context because that felt a little too academic. * Leave the order that positional fields are checked in == unspecified. * Clarify that positional fields are not sugar for named fields. Specify the evaluation order of fields.
The records spec does not currently use the word 'const' or 'constant'. Can a record be const? Something like
The text was updated successfully, but these errors were encountered: