Skip to content

Should structs and extension structs be tied together? #2361

Closed
@leafpetersen

Description

@leafpetersen

The extension struct proposal here ties together structs and extension structs in two ways: it uses overlapping syntax (the struct keyword), and it has a goal that the constructs may be thought of as not adding new functionality, but rather simply subtracting functionality from a more general construct in order to enable new affordances. So structs may be thought of for the most part as restrictions on classes, and extension structs as restrictions on structs. This is not entirely true, since at least as proposed, primary constructors are new syntax, and there are semantic deviations around identity (in the case of structs) and runtime representation in general (in the case of extension structs), but to a first approximation it is true. A disadvantage of this is that it requires finessing around competing priorities: some restrictions we may wish to impose on structs aren't really necessary for extension structs, and vice versa. An alternative approach is to split out the constructs into two or three separate features, breaking the connection, and giving greater flexibility to the design space. In the most general form, we'd split this into three separate features:

  • Primary constructors (for classes, data classes, and views)
  • data classes (aka structs in this proposal) which can (or must?) use primary constructors.
  • view classes (aka extension structs in this proposal) which can (or must?) use primary constructors.

We could also choose to restrict primary constructors to just data classes and views, to avoid complexities around supporting primary constructors for general classes.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions