-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Error final field must be initialized in constructor even though null is the default #3213
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
Correct me if I'm wrong Terry, but it looks like this is VM issue not a dart2js issue. I've changed the area label accordingly. Removed Area-Dart2JS label. |
This comment was originally written by @mhausner I think the VM handles this correctly. The spec says: Each final instance variable f declared in the immediately enclosing class must have an initializer in k's initializer list unless it has already been initialized by one of the following means: Consider the class given above: class X { Final instance field a does not have an initializer at the declaration. Thus, it must be initialized by the initializer list of the constructor. Constructor X([this.a]) initializes a, either to an explicitly given value, or implicitly to null if no parameter is passed to X(). Constructor X.constant() however does NOT contain final field a in its initializer list, thus the VM correctly complains that a is not initialized. If X.constant() redirects to X(), final field a is initialized, as if X() is called directly. The VM handles all this correctly. Gilad confirms my understanding. Added Invalid label. |
This comment was marked as spam.
This comment was marked as spam.
…2 revisions) https://dart.googlesource.com/dartdoc/+log/c2f66ecf1a75..b3856970e4ab 2022-10-10 [email protected] Remove 300 font weights, for improvements in dark mode (#3213) 2022-10-07 [email protected] Update README.md (#3209) If this roll has caused a breakage, revert this CL and stop the roller using the controls here: https://autoroll.skia.org/r/dart-doc-dart-sdk Please CC [email protected] on the revert to ensure that a human is aware of the problem. To file a bug in Dart Documentation Generator: https://github.com/dart-lang/dartdoc/issues To file a bug in Dart SDK: https://github.com/dart-lang/sdk/issues To report a problem with the AutoRoller itself, please file a bug: https://bugs.chromium.org/p/skia/issues/entry?template=Autoroller+Bug Documentation for the AutoRoller is here: https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md Tbr: [email protected] Change-Id: I94b8a054e92d5d95cf89754ef65bd6158039e72f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/263480 Commit-Queue: Devon Carew <[email protected]> Reviewed-by: Devon Carew <[email protected]> Commit-Queue: DEPS Autoroller <[email protected]>
class X {
final num a;
const X([this.a]);
const X.constant(); // Error "The final field a must be initialized"
}
If I change the X.constant constructor to be :
const X.constant() : this();
then it works.
Seems a little wierd that a is already assigned to null shouldn't it just do that. If I try to assign a outside of the initializer list then I'd expect an error can't change a final.
The text was updated successfully, but these errors were encountered: