Skip to main content

Metaprogramming

Morph provides macro, typeof, and static_assert for compile-time code generation and validation.


typeof

Get the type of an expression at compile time:

x is 42;
t is typeof(x); // "int"

static_assert

Assert a condition at compile time — compilation fails if the condition is false:

static_assert(sizeof(int) == 4, "int must be 4 bytes");

With a message:

static_assert(MAX_SIZE > 0, "MAX_SIZE must be positive");

macro

Macros generate code at compile time:

macro DebugPrint(expr) {
Print("Debug: " + typeof(expr) + " = " + expr);
}

When to Use

FeatureUse Case
typeofType introspection, generic helpers
static_assertCompile-time validation, API contracts
macroCode generation, boilerplate reduction

Next Steps

  • NIR — Morph Intermediate Representation