# Designated initialisers

> Why options are written as { .field = value }, why the fields of every options struct are declared in the order you would write them, and why a behavior has no base class.

Source: https://omardev29.github.io/rmp-docs/framework/why/designated-initialisers.html

Almost everything in the framework that takes options takes a plain struct, and the struct is written with C++20's designated initialisers: `{ .speed = 320 }`. It is the closest C++ has to named arguments, and it is why the API reads the way it does. It comes with one rule the language imposes, and the framework is arranged around that rule so you rarely meet it.

## What it buys

A call says what each value is, leaves out everything it does not change, and keeps working when a new option is added:

```cpp
auto &wall = spawn({ .position = { 400, 225 }, .size = { 20, 200 } });
wall.solid = true;
wall.immovable = true;

auto &player = spawn({ .position = { 100, 300 }, .size = { 24, 40 }, .layer = 1 });
player.add<rmp::behavior::TopDown>({ .speed = 220 });
```

The same shape runs through the whole framework: what [`spawn()`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Scene.html#spawn) takes ([`rmp::ObjectOptions`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp.html#ObjectOptions)), every container and widget of `rmp::ui`, the volume and pitch of a sound, the options of a save, a raycast's query, and every behavior in the catalogue. Each field has its default written next to it in the header, so the defaults are visible in the one place you would look.

## Declaration order

The rule: C++20 requires designated initialisers to be written in the order the fields are declared. Here the struct declares `grow_x`, `width`, `grow_y`, `height`:

```cpp
rmp::ui::begin();
rmp::ui::column({ .width = 240, .grow_y = true }, [] {
    rmp::ui::text("Inventory");
});
rmp::ui::end();
```

Written the other way round, it does not compile:

Does not compile

```cpp
rmp::ui::begin();
rmp::ui::column({ .grow_y = true, .width = 240 }, [] {
    rmp::ui::text("Inventory");
});
rmp::ui::end();
```

The compiler refuses it, saying among other things: `declaration order`

GCC and MSVC refuse it on their own. Clang, left to itself, only warns (`-Wreorder-init-list`) and initialises the fields in declaration order anyway — and Clang is what `[dev] compiler` builds your debug builds with unless you choose another, while CI builds the Windows x64 release and the NetBSD one with GCC. A game that built clean on the laptop would fail on those builds twenty minutes into CI.

So the framework makes Clang refuse it too, in each of the three build systems. The `rmp` library carries `-Werror=reorder-init-list` for Clang and AppleClang as a public option, which every game, example and test that links it inherits, and Android's native build, which the NDK's Clang compiles, carries the same flag.

The iOS app is built by Xcode from a project of its own, and `configure.py` writes the same flag into that project's `OTHER_CPLUSPLUSFLAGS`, ahead of any of the game's own in `[ios.settings]`, which are kept after it. Your own build gives the answer above, and so does every build in CI that could have disagreed with it.

A configure that builds the tests checks that this holds: it reads the options back off the `rmp` target and compiles one initialiser out of order and the same one in order with them. The first must fail and the second must compile, or the configure stops and says which.

Because the rule cannot be switched off, every options struct in the framework is declared in the order its fields are most likely to be written, rather than alphabetically or by importance: an image's options start with its size, a button's with its style, a container's with its gap and padding. In `rmp::ui`, an `id` is the last field of every struct that has one. And the sizing fields of a container are interleaved by axis, `grow_x, width, grow_y, height`, rather than grouped by kind: grouped, the most ordinary layout there is — a fixed-width sidebar that fills the height, `{ .width = 240, .grow_y = true }` — would be illegal. This way round every combination is in order.

## Only an aggregate can be written this way

Designated initialisers work on an *aggregate*: a struct with public fields and no constructors, no virtual functions and no virtual or private base. Give a struct a virtual function and the syntax stops working, with an error both compilers agree on:

Does not compile

```cpp
struct Spin {
    virtual ~Spin() = default;
    float degrees_per_second = 90;
    void _update(rmp::Object &self, float delta) {
        self.rotation += degrees_per_second * delta;
    }
};

Spin fast{ .degrees_per_second = 180 };
```

The compiler refuses it, saying among other things: `non-aggregate type`

That is the whole reason a [behavior](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html#add) inherits from nothing. The obvious design — a `Behavior` base class with a virtual `update` — would have turned `add<TopDown>({ .speed = 220 })` into a constructor taking an options struct, and every later read into something like `get<TopDown>()->options.speed`. Without the base class, the fields *are* the configuration, and they stay live:

```cpp
add<rmp::behavior::TopDown>({ .speed = 220 });
get<rmp::behavior::TopDown>()->speed = 400;
```

A behavior with a virtual function would compile up to the moment someone wrote its options, so `add()` refuses one outright, with a sentence that says why. The hooks a behavior may have are found at compile time with `requires` instead of through virtual functions.

The same reasoning is why the small struct an [`rmp::Sprite`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Sprite.html) uses to track its animation is public: a private member would stop the sprite being an aggregate.

## Through a template, the type needs a name

A braced list on its own has no type, and a function template that forwards its arguments cannot deduce one for it. So data passed to a scene through [`change()`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Scene.html#change), `push()` or `replace()` cannot be a bare braced list:

Does not compile

```cpp
rmp::Scene::change<GameScene>({ .level = 3, .hard = true });
```

The compiler refuses it, saying among other things: `no matching function for call to`

Give the struct a name and it works, designators and all:

```cpp
class GameScene : public rmp::Scene {
public:
    struct Start {
        int level = 1;
        bool hard = false;
    };
    explicit GameScene(Start start) : _start(start) {}

private:
    Start _start;
};

void play_level_three() {
    rmp::Scene::change<GameScene>(GameScene::Start{ .level = 3, .hard = true });
}
```

That is C++, not a decision of the framework's. `spawn()` does not have the problem, because it is not variadic: it takes an `rmp::ObjectOptions`, and the braced list knows what to become.
