Skip to content

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

Closed
terrylucas opened this issue May 24, 2012 · 4 comments
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. closed-invalid Closed as we don't believe the reported issue is generally actionable

Comments

@terrylucas
Copy link
Contributor

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.

@kasperl
Copy link

kasperl commented May 25, 2012

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.
Added Area-VM, Triaged labels.

@iposva-google
Copy link
Contributor

Set owner to @mhausner.
Added Accepted label.

@DartBot
Copy link

DartBot commented Aug 2, 2012

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:
 - Initialization at the declaration of f.
 - Initialization by means of an initializing formal of k.
or a compile-time error occurs.

Consider the class given above:

class X {
  final num a;
  const X([this.a]);
  const X.constant(); // Error "The final field a must be initialized"
}

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.

@terrylucas terrylucas added Type-Defect area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. closed-invalid Closed as we don't believe the reported issue is generally actionable labels Aug 2, 2012
@nairagrigoryan

This comment was marked as spam.

copybara-service bot pushed a commit that referenced this issue Oct 10, 2022
…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]>
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. closed-invalid Closed as we don't believe the reported issue is generally actionable
Projects
None yet
Development

No branches or pull requests

5 participants