Skip to content

Specify order that instance field initializers run with augmentations #4573

@munificent

Description

@munificent

The language says that the initializers for instance fields run in the order their declarations appear:

int i = 0;

class C {
  int a = i++;
  int b = i++;
}

main() {
  var c = C();
  print('${c.a} ${c.b}');
}

This prints 0 1.

Now consider:

int i = 0;

class C {}

augment class C {
  int a = i++;
}

augment class C {
  int b = i++;
}

Does it behave the same? Consider that these augmentations may be in separate part files.

I think the obvious answer is that field initializers run in augmentation application order. Or, said another way, applying an augmentation appends new members to a type. That's consistent with how enum values and mixin with clauses are handled.

I don't think there's anything scary here, the proposal just needs to actually specify this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    augmentationsIssues related to the augmentations proposal.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions