Unions

A union in Odin is a discriminated union. A union comprises of variants.

Frog :: struct {
    jump_height:   f32;
    ribbit_volume: f32;
};
Door :: struct {
    openness: f32;
};
Monster :: struct {
    speed:    f32;
    evilness: u8;
};


Entity :: union {
    Frog, Door, Monster, int, f32, bool,
}

e: Entity;

// Implicit conversion from variant to union type
e = Frog{
    jump_height   = 2.3,
    ribbit_volume = 0.1,
};

// A variant is a separate type from the parent union
f: Frog;

Checking to see which variant a union can be achieved through the use of a type assertion or a match in statement.

results matching ""

    No results matching ""