Skip to content

Inconsistency for init this; between unions and records/classes #29140

Description

@bradcray

In the 11:59th hour of developing #28984 I realized that, while in my mind, Chapel automatically injects an init this; statement into the initializer of a record/class after I've initialized all of the fields, the language spec and a few tests show that I'd mis-internalized this:

If the user does not use `init this` explicitly, the compiler will insert it at the end of the initializer.

Rationale.
Due to support for omitted field initialization, there is potential for confusion regarding the overall status of initialization. This confusion is addressed in the design by requiring init this to explicitly mark the transition between partially and fully initialized instances.

Implementors’ note.
Even if the user explicitly initializes every field, the init this statement is still required to invoke other methods.

That suggests that the approach we agreed to the other week—that the compiler would auto-insert an init this; after the first field assignment in a union initializer to transition from phase 1 to phase 2 is not as symmetric to records/classes as I was thinking.

Thinking about ways to resolve this, we could:

  • say that unions must also explicitly call init this;, either
    • generating a compiler error if multiple fields are initialized before init this;
    • or permitting any fields to be initialized in any order where, logically, only the last will win
  • say that unions are sufficiently different than records/classes that having the compiler auto-insert init this; is reasonable given that only one field needs to be initialized (i.e., the rationales above don't apply as much since the condition for "I am initialized" is simpler)
  • have the compiler auto-insert init this; for records/classes in certain well-behaved case (e.g., init this; is inserted whenever the last field is initialized)

This issue captures this discrepancy, with the intention of ideally resolving it before Chapel 2.10 is released.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions