Skip to content

Why are late instance fields not lazily initialized in constructors? #1115

@creativecreatorormaybenot

Description

After having dealt with late for a long time now and finding the two different aspects of the late modifier to be convoluted (see #1100 and #1101), I totally expected constructor initialization to not be lazy. But why is that?

You can find a more detailed version of the question and code here.

class Baz {
  Baz(
    // Implicit assignment by the caller.
    this.assignmentInConstructor,
  ) : assignmentInInitializerList = calculate() {
    assignmentInConstructorBody = calculate();
  }

  late final int assignmentInConstructor; // I expect this to be lazily initialized.
  late final int assignmentInInitializerList; // I expect this to also be lazily initialized.
  late final int assignmentInConstructorBody; // I expect this to not be lazily initialized.
}

None of the instance fields are lazily initialized. I included what I thought to be the intuitive though of someone who knows constructors but has not dealt with late yet.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions