OxL Reference

15.4 User-defined attributes and code generation

oxl
attr Audit { level: Level = Level.Info }

comptime fn Audit.apply(d: Decl, a: Audit) -> Decl | CompileError {
  d.wrapBody(body => quote { log.at($a.level, "call {$d.name}"); $body })
}
  • attr Name { fields } declares an attribute; comptime fn Name.apply(d: Decl, a: Name) is the handler called for every declaration with this attribute.
  • std.comptime provides the AST (Decl, Expr, Type), quote { … } with $x substitutions and the CompileError diagnostic.
  • @derive(I) is implemented the same way. The generated code is checked as ordinary code.

Implementation-defined. Code generation by attributes (D-8052; D-7530). In this implementation attr Name { fields } declares a data type and @Name(…) works as a Middleware (§10.2); the handler comptime fn Name.apply(d: Decl, a: Name) that rewrites declarations is reserved and not implemented (an optional feature).

See also

Source: docs/spec/oxl-standard.en.md