dart-lang / dart-lang/language

Constant parameters

Open
#1,684 8 comments 20 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhanced-const feature
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

There is a request to add an annotation for parameters whose semantics ensures that the argument corresponding to the annotated parameter is a constant expression.

It occurred to me that this might be an interesting language feature. The proposal would be to allow the modifier const on parameter declarations and for the semantics to match those requested for the annotation. For example,

void f(const List<int> x) {}

void g(int a) {
  f(const [1, 2, 3]); // OK because it's explicitly const.
  f([1, 2, 3]); // OK because it's implicitly const.
  f([a]); // ERROR because `a` isn't a valid const expression
}

It might additionally provide some possibilities for compiler optimizations if it's known that the value of the parameter can't be modified.

I suspect that we'd want to introduce the annotation first and see how/whether it's used in practice, but I thought I'd mention it in case you'd want to track the work on the annotation.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the linked Dart SDK request and the examples in this issue. Determine whether the annotation should be pursued before a const parameter modifier, then document the accepted semantics and language-specification changes; no implementation file or test is identified here.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.