Skip to content

const correctness like in C++ #1567

Closed
Closed
@elsamuko

Description

@elsamuko

I'd love const correctness for parameters and methods similar to C++.
If I mark a method as const, it guarantees, that calling it won't change the instance.
If I mark parameters as const, it guarantees, that calling it won't change the parameters.
In C++ this feature is a great assistance to avoid side effects and helps the compiler to optimize.

Example implementation would be:

import 'dart:io';

class C {
  int i;
  int k;

  C(this.i, this.k);

  int sum() const {
    // i and k are not changed -> sum() can be const
    return i + k;
  }
}

int sum(const int i, const int k) {
  // i and k are not changed -> i and k can be const
  return i + k;
}

void main() {
  C c = C(2, 3);
  print(c.sum());
  print(sum(2, 3));
}

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