Skip to content

Latest commit

 

History

History
28 lines (23 loc) · 1.2 KB

File metadata and controls

28 lines (23 loc) · 1.2 KB

Closed Values and Structs

Structs can be marked with the closed modifier. This transforms them into discriminated unions and allows them to effectively be subtyped. The "subtypes" become members of the discriminated union. Closed structs are abstract. One cannot create an instance of them only of the subtypes.

A closed struct can only have a fixed set of subtype structs. Only structs can inherit from them, but see the Cases section for a shorthand syntax. Typically, these are declared inside the closed struct, though technically they can also be declared outside the struct. Private initializers can be used to prevent that if desired. The individual subtype structs can override members of the struct and add fields. The standard mechanisms of sealed, overrides, and hides can be used to control this process.

public closed struct Game_Entity
{
    // Common fields and initializer

    public struct Player: Game_Entity { /* ... */ }
    public struct Enemy: Game_Entity { /* ... */ }
    public struct Obstacle: Game_Entity { /* ... */ }
    public struct Animal: Game_Entity { /* ... */ }
}

// Struct declared outside of the closed struct
public struct Wall: { /* ... */ }