-
Notifications
You must be signed in to change notification settings - Fork 213
Should spread collections support const? #63
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
So as one of the biggest users of
If we were working on a general expansion of |
From a different viewpoint, not supporting this in const can be considered a special case. We're definitely accustomed to running up against limitations on what we can do in a const context, but having an entire syntax - not value or operation - off limits to me inside a literal would be surprising to me. |
I'm on the fence here. It's obviously doable. We are doing something similar with constant string interpolations. There is a (philosohical) difference between strings and lists: to embed a list in another, you need to deconstruct it, which is something we otherwise never do in const expressions. The distinction is philosophical, rather than real, because you actually also deconstruct strings into characters in order to embed it in another string, it's only because we see strings as atomic that it feels differently. I can argue consistency either way: Allow it, like we allow constant string interpolations, because it's really the same thing, or disallow it like we disallow any other attempt to deconstruct a list. I would require specifying exactly which lists/maps can be spread (only ones created by const list/map literals). |
There's a real distinction too, which is that String is final, but List is not. |
If by |
Ah, interesting. I see the spec says:
Given that prior art, it seems a little more reasonable to me to do a similar restriction for List inside spreads and allow that. |
Given that one of the main use cases motivating this feature is flutter and const is important to flutter, I'm fairly inclined to allow const spreads unless there are good reasons not to, or unless the flutter folks are opposed/don't think this would ever come up. @Hixie, @yjbanov any thoughts on this? |
I think all side-effect-free code should be allowed in const expressions. |
I agree with @Hixie that const expressions should be more expressive than they are right now. However, I'm not able to come up with any specific use-cases right now, so I can't comment on how important it is to implement this right away. |
OK, I'm convinced it's worth allowing |
The current "Spread Collections" proposal does not allow using spread in a const list. This is necessary in cases where the object you are spreading could do unexpected computation, like:
But in cases where the object being spread is an actual core library List object, the spread is safe. That's because it must be const (since it's inside a const context), and we have access to the actual value at compile time, not just its static type.
So we could say that you can spread inside a const list if and only if the spread object const-evaluates to a dart:core List.
Is this worth doing? Personally, I dislike adding yet more special cases for const shenanigans, and I rarely use const, so I'm not inclined. But if users feel it's important, I can see doing it.
The text was updated successfully, but these errors were encountered: