LEARN VOID / DATA & BEHAVIOR

Inheritance & dispatch

Extend a managed type and choose virtual behavior explicitly.

inheritance.voidDownload source ↓
struct base_user:
    health: i32 = 100

virtual func (u base_user) take_damage(amount: i32):
    u.health -= amount

struct player extends base_user:
    armor: i32

override func (p player) take_damage(amount: i32):
    super.take_damage(amount)
    p.armor++

func apply(u: base_user):
    u.take_damage(3)

func entry(value: i32) -> i32:
    var p = player(armor = value)
    var b: base_user = p
    apply(b)
    if b == p:
        return b.health + p.armor
    return 0
Input 5Expected result 103

Declare an extension

extends gives a managed struct one base type. A derived object keeps its identity when viewed through the base type.

Make dynamic dispatch visible

virtual marks a base method for dynamic dispatch, and override provides its derived implementation. super.take_damage calls the base implementation from the override. Ordinary methods do not become virtual automatically.

Follow a base view

apply receives the player through a base_user view. The override subtracts 3 from health and increments armor. Input 5 leaves health 97 and armor 6; the same-object check succeeds and the result is 103.