Skip to content
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

[proposal] add const at compile time (where possible) #56669

Closed
iapicca opened this issue Sep 9, 2024 · 4 comments
Closed

[proposal] add const at compile time (where possible) #56669

iapicca opened this issue Sep 9, 2024 · 4 comments
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-enhancement A request for a change that isn't a bug

Comments

@iapicca
Copy link

iapicca commented Sep 9, 2024

related issues

note quote from the former

In theory, const-ness should give apps a performance boost, but during development the lints enforcing const are constantly nagging developers: "Make this const" or "This cannot be const anymore".

proposal

if a linter is able to tell if a variable, a constructor or (in flutter) a child should be a constant,
wouldn't be more developer friendly to automatically make it a constant at compile time
without (quote) "nagging developers"

@dart-github-bot
Copy link
Collaborator

Summary: The issue proposes automatically making variables, constructors, and Flutter children const at compile time if a linter can determine they should be constant. This aims to improve performance without developers needing to manually add const annotations.

@dart-github-bot dart-github-bot added area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-enhancement A request for a change that isn't a bug labels Sep 9, 2024
@eernstg
Copy link
Member

eernstg commented Sep 9, 2024

if a linter is able to tell if a variable, a constructor or (in flutter) a child should be a constant,

This is not possible, it would at most be able to say that it could be constant, based on a local analysis. The reason for this is that it can make a difference semantically whether any given expression is marked with const or not, and it's a matter of undecidable properties (like "will we ever perform an identity test on this object, and will we expect canonicalization to make it true?" or "will we ever attempt to add an element to this list?").

void main() {
  var xs = <int>[]; // means `const <int>[]`?
  xs.add(1); // Succeeds if `<int>[]`, throws if `const <int>[]`.
}

This is exactly the reason why I'm proposing that a developer must use their knowledge about the design of the enclosing software and make the choice to put a const? on an expression, and only then is it OK for the compiler/analyzer to make the choice about having or not having const on certain subexpressions. The developer who puts that const? in place is promising that it isn't a bug to have const on every one of those subexpressions, if possible, and also that it isn't a bug if there is no const. That's a non-trivial promise, but I expect that we can develop a community awareness of the situations where it is indeed not a bug to add/remove those const markers implicitly. For instance, it's probably fine for most Flutter widget building expressions.

@mateusfccp
Copy link
Contributor

The issue dart-lang/language#1410 has more discussion about the implications of this request.

@rakudrama
Copy link
Member

Another approach is to change the language to have more contexts that are const if possible.
See dart-lang/language#3399

TL;DR:

The lints have quick-fixes to help with this process, but pushing const around the code would be completely unnecessary if the widget constructors defaulted to const whenever possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

6 participants