Skip to content

Constant parameters #1684

@bwilkerson

Description

@bwilkerson

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhanced-constRequests or proposals about enhanced constant expressionsfeatureProposed language feature that solves one or more problems

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions