-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Mutation of const List should be compile time error #34551
Comments
This is not a compile-time error because Dart doesn't track whether operations mutate an object. To figure out that this is an error, the analyzer would have to know that It's not something we plan to build into the language in general. It's too specific - it would only work for constant lists and maps, and only when referenced directly as a const variable, and hard-coding specific members in the specification is just begging to get out-of-date - so I'm filing as an analyzer feature request. |
I understand. |
By the way, setters are compile time errors. |
The difference is that assignment to a class SetLog {
const SetLog();
void operator[]=(String key, String value) {
print("LOG($key): $value");
}
}
main() {
const s = SetLog();
s["line 1"] = "Error!";
} This program compiles and runs, even though it "assigns" to the So, to reject a program, the language needs to know that a library function will always throw on a particular invocation, and the language specification doesn't know that much about the libraries. |
Sorry, I misunderstood the behavior of setter. class Hello {
const Hello();
set hello(String value) {
print("Hello $value");
}
}
main() {
const h = Hello();
h.hello = "world"; // => Hello world
} |
The snippet above doesn't emit compile time error but runtime exception.
I'm not sure, but think this issue is related to #27755, #23327, #1869 and #236.
That said, In constant context, I also think it is much easier to fix along with Dart 2 compiler, because const object/ identifier is deeply const.
Dart VM version: 2.1.0-dev.5.0 (Unknown timestamp) on "linux_x64"
The text was updated successfully, but these errors were encountered: