Skip to content

Should structs support sub-classing? #2367

Closed
@leafpetersen

Description

@leafpetersen

In the struct proposal (#2360), I propose that structs be forbidden from extending concrete structs, though I permit extension of abstract structs. There is a brief discussion of allowing more general extension at the end of the proposal. This issue is to discuss the general question of whether to allow structs to extend other concrete structs, and if so, in what fashion? Concrete questions include:

  • May a concrete struct extend another concrete struct?
  • May a concrete struct override fields from its concrete super-struct?
  • May a concrete struct add additional fields?
  • May a struct extend another struct from a different library?

My proposal was designed to meet certain goals, which are useful to review for this discussion. These are not hard requirements necessarily, but they are relevant to the discussion. Specifically:

  • It is desirable that access to struct fields can be reliably compiled to simple memory accesses.
    • This pushes against making them virtual (at least outside of the current library): while whole program analysis can detect which fields are/are not overridden, it still means that code outside of the control of a library author can substantially impact the performance characteristics of the library.
  • It is desirable that access to struct fields can be reliably compiled to simple memory accesses even in a modular compilation setting.
    • This pushes against making fields virtual outside of the current library.
  • It is desirable that the memory layout (and specifically the size in memory) be predictable from the declaration to enable reliable unboxing (again, including in a modular compilation setting if possible).
    • This pushes against both virtuality, and allowing sub-structs to add fields.
  • It is desirable that field accesses on structs be promotable.
    • This pushes against allowing struct fields to be implemented via getters (i.e. virtual).

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions