-
Notifications
You must be signed in to change notification settings - Fork 213
Allow a const
record entry to be assignable to a const
#3004
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
Comments
const
record entry to be assignable to a const
variableconst
record entry to be assignable to a const
In principle, yes, this is a thing we could do. Since records can't be implemented or inherited from, we know that every record field access is effectively non-virtual. If we know that the record the field is being accessed on is constant, then we can know that the field access is itself constant too. |
Note the overlap with #2219 (comment). |
I really wish this was shipped with 3.0. I wanted to write an inline class with a const constructor that accepts records and turns them into a bitflag: const RelativeBoxConstraints({required Dim2D<bool> flexible}) : _flags = (flexible.width ? 1 : 0) | (flexible.height ? 2 : 0); Of course the workaround here is to use separate named parameters for width and height but that simply isn't as clean. As a workaround, I can use operator ==. But that's a horrible solution to this problem. |
There is a number of member accesses we can allow at compile-time.
I'm pretty sure we have a request for "more const expressions" somewhere, I just can't find it. (Damn you, Github search!) (Before someone says "final fields", then no, a final field is not a promise to not make it a getter in a later version of the same code. Allowing accessing fields, but not getters, would make it one. #299) |
This does seem like an obvious fit for const expressions. const r = (x: 1, y: 2);
const x = r.x, y = r.y; It might even be possible to allow a const r = (x: 1, y: 2);
const (:x, :y) = r; |
If a record is
const
, and its entries must beconst
, could we get a way to assign it to a constant? Unfortunately I think a const return (const Color get primary
) would need to be present on the getter method. Could we under the hood create aconst
instance and replace it inline? This may break some things.The previous code would generate something like this:
The text was updated successfully, but these errors were encountered: