Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Entity references

An attribute of type ref identifies another entity instance by its UUID. The reference is type-erased: the generated API knows that it is an entity reference, but does not restrict which entity type it targets. This is useful when any kind of entity is a valid target.

YAML model

quent: alpha
model: untyped_references

entities:
  Worker:
    events:
      started: {}
      ended: {}

  Task:
    events:
      started:
        attributes:
          source: ref
      ended: {}

Instrumentation API

Every entity handle exposes its identity as a type-erased reference. Here, the task’s started event receives a reference to the Worker instance.

use instrumentation::{Context, Noop, Task, UntypedReferences, Worker};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let context = Context::<UntypedReferences>::try_new(Noop)?;

    let mut worker = context.observer::<Worker>().handle();
    worker.started()?;

    let mut task = context.observer::<Task>().handle();
    task.started(worker.as_any_entity_ref())?;
    task.ended()?;

    worker.ended()?;

    Ok(())
}
#include "quent-tutorial-untyped-entity-references-cpp-bridge/gen/quent.hpp"

int main() {
  auto context = quent::Context::none();
  auto worker = context.worker_observer()->handle();
  worker.started();

  auto task = context.task_observer()->handle();
  task.started(quent::task::Started{.source = worker.id().raw()});
  task.ended();
  worker.ended();
  return 0;
}
import quent_tutorial_untyped_entity_references as quent


def main() -> None:
    with quent.Context() as context:
        worker = context.worker_observer().handle()
        worker.started()

        task = context.task_observer().handle()
        task.started(source=worker.id)
        task.ended()
        worker.ended()


if __name__ == "__main__":
    main()

When an attribute must target a particular entity type, the Reference Target semantic module adds that restriction to the generated API.

Key point

A type-erased reference preserves an entity's identity without restricting its entity type.

Check yourself

What does a type-erased entity reference preserve?

Does a bare ref require its target to be a Worker?

Full code