Section 7.2

A shape, and its instances

archetype declares the shape; the program makes as many instances of it as it wants.

An object exists as a single instance — that is its definition, laid down back in section 1.2. An archetype is the other half of the promise: a shape, of which you make as many instances as you want.

archetype Character {
    name: str;
    energy: int;
}

@entry(start)
object MyProgram {
    start() {
        var simon := Character(name: "Simon Magus", energy: 10);
        var helen := Character(name: "Helen", energy: 7);

        Console.print(text: $"{simon.name}: {simon.energy}");
        Console.print(text: $"{helen.name}: {helen.energy}");
    }
}

The shape

archetype Character {
    name: str;
    energy: int;
}

Two fields, each with its name and type — the same writing as a method’s parameters. The archetype holds no value yet: it only says what a Character is made of. It is a mould, not a statue.

The instances

var simon := Character(name: "Simon Magus", energy: 10);
var helen := Character(name: "Helen", energy: 7);

The archetype’s name, followed by the named fields — the writing of section 1.2.1, applied to fields. Each call builds a fresh instance with its own values: Simon and Helen share nothing. A field is read through the dot: simon.name.

No keyword to construct: naming the shape is enough.

An omitted field is zero

Construct without giving the energy:

var simon := Character(name: "Simon Magus");

The program runs, and simon.energy is 0. Construction does not demand every field — what is not given takes the absence marker, the same zero section 5.2 invoked for returns. An omitted text is the empty string, an omitted number is zero: nothing appears without an origin.

= compares identity, not content

Two instances built with the same values are still two instances:

var a := Character(name: "Simon Magus", energy: 10);
var b := Character(name: "Simon Magus", energy: 10);

a = b is false. That is the opposite of texts, which section 3.1 compares by content — two instances are only equal when they are the same one. Comparing characters field by field is yours to write, with what you already know.

Your turn

  • Add a third character and print it.
  • Omit energy: when building Simon: zero, and no error.
  • Write if (simon = helen) then if (simon = simon): the first is false, the second true. = asks “is it the same one?”, never “do they look alike?”.
  • A Character(name: "X", energy: 10) = Character(name: "X", energy: 10) — two brand-new instances, identical field for field: still false.