-
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
const constructors can't have asserts #24841
Comments
First of all, I would not use Second, you are using (Another solution would be to allow throwing in potentially compile-time constant expressions, then a "throw not-compiler-time-const-expression" would be a compile time error if reached during const construction, and a runtime throw if reached when called using |
We don't want to do this in production code because these checks can be quite expensive. What we really want is static checks at compile or analyze time, but for some of the checks we really just have to run code (e.g. inspecting maps for consistency, checking trees for duplicates, etc). We want const for both enforcing final fields and compile-time construction. We're open to any solution, obviously; compile-time asserts are just the shortest obvious path from what we do now to something that would address our needs:
We're happy to ditch |
To give some context here: where we can use const, we can get upwards of 10x performance improvements over where we can't use const. The only thing stopping us from making huge parts of the Flutter framework const are the fact that we can't put asserts in const constructors. |
Any solution to asserts in const constructors will not allow you to check maps for consistency or trees for duplicates in a const constructor - there is still a "no general code executed at compile-time" restriction. You can only check that a compile-time constant expression is true at compile-time. |
We're open to any solution, the problem space and our priorities within that space is what is described in my Nov 6, 2015 comment. As you say, pure const asserts wouldn't solve everything in that list. It would solve everything except the second bullet of the first bullet, I think. That's a huge improvement over where we are now. Of course if we can do even more, that would be even better. :-) |
I have landed an experimental implementation of To make it really useful, you probably need to wait for the analyzer to recognize the syntax (#27142). |
The analyzer bug is #27142 (for anyone else following along at home) |
Tracking issue for implementation: #27141 |
This is done, yes? Can we close this? |
There's still some work to do for all of our tools to support this (#30968) but I'm going to go ahead and close this now since Flutter has what it needs and we have a tracking bug for the rest. |
In Flutter we are trying very hard to make our Widget subclasses all have const constructors, because we want to make that entire class hierarchy be immutable by convention, and the rule "you have to have a const constructor" makes it easy to verify that you didn't screw that up. (It also means that widget subtree descriptions, even relatively elaborate ones, can end up being all evaluated at compile time rather than runtime.)
We are also trying very hard to catch errors in arguments passed to Widget classes as close to the relevant source line as possible. The best way we've found to do this is to have asserts in the constructors. We're checking all kinds of things -- that your arguments aren't null, that they're in the right numeric range, that you have provided either this or that argument but not both (or not neither), that the map you passed in has the important keys we need, etc.
Currently these two desires are in conflict. You can't have any asserts if your constructor is const, even if the assert condition itself is a compile-time-evaluatable const expression.
(Track implementation here: #27141)
The text was updated successfully, but these errors were encountered: