Skip to content

Compile-Time Attribute Macros

Status: Deferred. This is a significant design piece and will be addressed in a dedicated revision. The outline below captures intent and open questions.

@[Attribute] can trigger compile-time code generators that inspect the annotated type or method and emit additional code. This is the foundation for Rails-style declarative APIs with full type safety.

The model follows ActiveRecord’s approach — fields are derived from a defined database schema, not declared in the class. The class stays clean, declaring only relationships, validations, and behaviour:

class Post < ApplicationRecord
belongs_to :user
has_many :comments
validates :title, presence: true
# title, body, created_at, updated_at etc. are synthesised at compile time
# from the database schema — not declared here
end

The compile-time macro on ApplicationRecord reads the schema and generates typed field accessors on the class. User code never declares fields manually.

This differs from Entity Framework’s approach where fields are annotated individually on the class — Amethyst’s ORM story is schema-first, model-second.

  • How do attribute macros declare what code they generate?
  • Do they receive a typed AST to inspect, or a simpler reflection API?
  • Can they generate new methods, new classes, or both?
  • Ordering — can one macro depend on the output of another?
  • Error reporting — macros should produce actionable diagnostics
  • How are user-defined macros packaged and distributed?