Inheritance

A struct may extend one base struct, inheriting its storage and accessible behavior while adding its own fields and members.

Declaring a base struct

Place the base type after :. Xenon uses single struct inheritance: a derived struct has at most one base struct.

struct Entity
{
    public int Id;
}

struct Enemy : Entity
{
    public int Health;
}

Enemy contains both Id and Health. Public base members are available through the derived value; private members remain internal to the struct that declares them.

Layout

The base struct storage begins at offset zero in the derived layout. Base fields are physically part of every derived value, followed by storage introduced by the derived struct according to the target ABI layout.

Inheritance and allocation

The complete derived value may live on the stack, inline inside another value or in heap memory. Inheritance affects its layout, not where it must be allocated.

Base constructors

A derived constructor selects a base constructor with : base(arguments). Base construction completes before the derived constructor body runs.

struct Entity
{
    public int Id;

    public Entity(int id)
    {
        Id = id;
    }
}

struct Enemy : Entity
{
    public int Health;

    public Enemy(int id, int health) : base(id)
    {
        Health = health;
    }
}

When a derived constructor omits an explicit base call, Xenon selects an available parameterless base constructor.

Pointer and reference conversions

A derived pointer or reference converts implicitly to the corresponding base pointer or reference. The conversion preserves the identity of the original derived object.

Enemy enemy = Enemy(7, 100);

Entity& entity = enemy;
Entity* pointer = &enemy;

int id = entity.Id;

The reverse base-to-derived conversion is not implicit.

Destructor order

Destruction proceeds from the most-derived struct toward its bases. The derived destructor body runs first, after which Xenon invokes the base destructor automatically exactly once.