Closed
Description
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));
}