-
Notifications
You must be signed in to change notification settings - Fork 214
Can't use static const of class in other static const #1868
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
Comments
You are trying to use a non-constant to make a constant. |
@ltOgt, you can't access members on any object, only classes. |
Thanks @mateusfccp + @Levi-Lesches |
Field accesses are not constant. One of the reasons is that something being "a field" is not part of a class's API. From the outside, it's a getter, which is what allows you to freely change a field to a getter and vice-versa. If you could declare an instance |
@lrhn I think I still don't fully understand this. I.e. is |
It is not guaranteed that any getter invocation on an object is "constant", even when that object was obtained as the value of a constant expression: class A {
final bool b;
const A(this.b);
}
bool _b = true;
class B implements A {
bool get b => _b = !_b;
const B();
}
void main() {
const A = B();
print('${A.b} != ${A.b}'); // Prints 'false != true'.
} The point is that there's nothing stopping "a constant object" from running arbitrary code. |
The proposal about stable/final getters is aimed at this exact property, that is, adding true semantic immutability to Dart. The trade-off is that if you declare that a specific getter is stable or final then it becomes a compile-time error to override it the way it's done in the class |
Thank you for the example, the override in |
If your question's been answered, you can close this issue to help clear up the list of open issues. |
Take the following:
which shows the problem
is this by design or should this be possible?
The text was updated successfully, but these errors were encountered: