The motivation document explains why we are working on static metaprogramming. This proposal introduces macros to Dart. A macro is a piece of code tha

Search code, repositories, users, issues, pull requests...

submited by
Style Pass
2024-12-02 16:30:04

The motivation document explains why we are working on static metaprogramming. This proposal introduces macros to Dart. A macro is a piece of code that can modify other parts of the program at compile time. A macro application invokes the given macro on the declaration it is applied to. The macro introspects over the declaration it was applied to and based on that generates code to modify the declaration or add new ones.

A macro declaration is a user-defined Dart class that implements one or more new built-in macro interfaces. Macros in Dart are written in normal imperative Dart code. There is not a separate "macro language".

You can think of macros as exposing functionality similar to existing code generation tools, but integrated more fully into the language.

Most macros don't simply generate new code from scratch. Instead, they add code to a library based on existing properties of the program. For example, a macro that adds JSON serialization to a class might look at the fields the class declares and from that synthesize a toJson() method that serializes those fields to a JSON object.

Leave a Comment