Meta-Programming and Macro capabilities of various languages

  • Автор темы Автор темы Sascha
  • Дата начала Дата начала

Sascha

Команда форума
Администратор
Ofline
https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0ifr5rqf4y6ur723rn0g.png


Meta-programming = the broad idea of “programs that manipulate or generate programs”. It can happen at runtime (reflection) or compile-time (macros).

Macros = one specific style of meta-programming, usually tied to transforming syntax at compile time (in a pre-processor or AST-transformer). It takes a piece of code as input and replaces it with another piece of code as output, often based on patterns or parameters.

  • Rule‑based transformation: A macro is specified as a pattern (e.g., a template, an AST pattern, or token pattern) plus a replacement that is generated when that pattern is matched.
  • Expansion, not function call: Macro use is not a runtime call; the macro is expanded before execution, so the final code is the result of replacing the macro invocation with its generated code.

Here are some programming languages and their meta-programming and macro capabilities.

NB! Take with a grain of salt. The result comes from working with perplexity.ai, and I have not had a chance to personally verify all of the cells. They do look generally correct to me overall, though. Corrections are welcome!

Metaprogramming + macro features​


Here are the programming languages with their scores (out of 18) and links to their repos or homepages:

Scores are out of 18 = 4 (metaprogramming) + 3 (compile‑time facilities) + 11 (macro features).

Each cell is either (yes) or (no / limited).

Feature / languageRacketScheme (R7RS‑small)Common LispClojureRustNimZig comptimeJaiC++RubyCarp
Metaprogramming:
Runtime metaprogramming (e.g., open classes, define_method, method hooks)
Runtime reflection / introspection
Runtime eval / dynamic code loading
Build‑ or tooling‑level code generation supported
Metaprogramming score (out of 4)44431111231
Compile‑time facilities (not strictly macros):
Run arbitrary code at compile time✅ (constexpr)
Types as values at compile time✅ (– but in Typed Racket)✅ (constexpr + templates)
constexpr‑style type‑level / compile‑time computation✅ (const‑eval)✅ (via constexpr)
Compile‑time facilities score (out of 3)32203333302
Macros:
Hygienic identifier binding
Operate on AST / syntax tree
Pattern‑based transformations
Define new syntactic forms
Define new keywords / syntax
Override core language forms
Multi‑phase / macros of macros
Full‑fledged DSL / language building (via macros)
Template metaprogramming
Macro features score (out of 11)11101088844100
Total score (out of 18)16151412101077542

The score counts one point per row where the language can reasonably do what the feature describes (DSL‑building is counted as a full feature, even if “limited” in some languages).

The feature score is not an ultimate measure of meta-programming power, since a language (like C++) may have a higher score than another language (like Ruby), but generally be considered less tailored for meta-programming than the other language (Ruby is generally revered for its powerful meta-programming abilities).

Macro features are varied and many, and thus in the total score they gain an undue weight, although runtime meta-programming may be just as, or even more, powerful.

 
Назад
Сверху Снизу