Skip to content

CEP-0009: Generated Declarations and Impls

Draft

Draft proposal for generating declarations or impls from comptime code. V1 comptime cannot inject declarations, rewrite source, or generate impls.

Summary

Catalyst should eventually decide whether comptime code can produce declarations or implementation declarations as first-class semantic output.

V1 comptime produces values, diagnostics, types, reflection results, and explicit metadata mutation through attribute target setter APIs. It does not inject names into namespaces or rewrite source.

Example

Possible future shape:

comptime generate_fields(Point, fields: .x, .y) {
  emit fn zero() Point {
    return Point { x: 0, y: 0 }
  }
}

const origin = Point.zero()

The exact spelling is intentionally unsettled. The important design question is whether generated declarations are explicit semantic outputs that tooling can inspect.

Motivation

Generated declarations are a tempting replacement for macros and template code. They can reduce boilerplate for repetitive impls, adapters, reflection tables, and bindings.

The risk is hidden source shape. If generated declarations exist, tooling must be able to show where they came from, what they generated, and why they are part of a namespace.

Proposed Direction

Generated declarations and impls should be explicit semantic outputs, not arbitrary source rewriting. The design should answer:

  • which comptime contexts may generate declarations;
  • where generated declarations appear in namespace order and documentation;
  • how generated names interact with visibility, imports, and re-exports;
  • how generated impls interact with coherence and conformance visibility;
  • how diagnostics, navigation, formatting, and incremental rebuilds report generated output;
  • whether generated declarations are cached as semantic products or re-expanded for tooling display.

Generated code should remain inspectable. A user should be able to ask the compiler or IDE what declaration was generated, from which source location, and with which recorded dependencies.

V1 Compatibility

V1 remains unchanged: forced comptime blocks and functions cannot inject declarations or impls. Attribute providers may mutate explicit target metadata through phase-owned setter APIs; that is the V1 exception, not general generation.