<p>For example instead of macros Zig has comptime feature that allows codegen like implementation of arbitrary arity functions while being native Zig code and not some separate macro language that isn't generally aware what given piece of code means in context of target language.</p>

<meta-programming>
What alternatives to macros there is for meta programming on compile time?
CC BY-SA 4.0
2023-05-18T08:13:46.377Z
Do you mean just code generation, or is e.g. reflection included in "meta-programming"? It would help to narrow this question down as it's quite broad.
kaya3
CC BY-SA 4.0
2023-05-18T08:43:21.733Z
Given that macros can be extremely wide as a concept, encompassing things like compile time execution, this is an ill-posed question. You can implement Zig style CT execution and call it a macro system.
Nuoji
CC BY-SA 4.0
2023-05-28T14:15:17.727Z
Would this question be off topic if it was simply "what are methods to implement compile time meta-programming?"
We have many list possible design choices questions of that type already which seem to be on-topic
Bruce Adams
<h2>Preprocessor/Parser Plugins</h2>
<p>Instead of preprocessing your code, you just make your parser extensible with a plugin system. This system could allow plugins to convert custom syntax to AST of it's choice, or just convert to to different syntax for later stages of the parser to handle.</p>
<p>The advantage here is extremely flexibility. Instead of macros being limited to only function call syntax or using <code>!</code> identifier you can create completely new operators and syntactic constructs. You could even use it to disable some syntax you don't like or enforce linting rules.</p>
<p>Disadvantage is that it's less integrated with your source. Plugins would need to be built separately so your compiler can access them at compile time. It would also make it harder to learn your language if different projects can add different bits of syntax.</p>
<p>This is the approach used in the <a href="https://babeljs.io/" rel="noreferrer">Babel javascript transpiler.</a>. It's also common in PHP where libraries overwrite the autoloader for similar effect. (though it's more like a preprocessor in that case)</p>

CC BY-SA 4.0
2023-05-18T09:02:12.810Z
2023-05-18T09:02:12.810Z
mousetail 'he-him'
CC BY-SA 4.0
2023-05-18T12:27:33.050Z
This is the way Java and Kotlin (and other JVM langs I think) do it
Seggan
<p>In Smalltalk, you do meta-programming by writing code that generates classes and methods. This is easy to do because classes are not syntactic but dynamic entities. Put differently, a class is not a piece of text but a data object in memory. To create a new class from Smalltalk code, you can for example send a message to class <code>Object</code> asking it to create a new subclass of itself. Next, you send a message to that new subclass asking it to compile a method into itself.</p>

CC BY-SA 4.0
2023-05-18T08:44:52.160Z
2023-05-18T08:44:52.160Z
khinsen
<p>Compile-time metaprogramming generally can be split in two approaches:</p>
<h1>Context-less</h1>
<p>Languages with context-less CT metaprogramming generally have some way to transform the source text of a file, but can't know what the code contains.</p>
<p>Often these don't even have ways (or <a href="https://stackoverflow.com/questions/6707148/foreach-macro-on-macros-arguments">need complicated workarounds</a>) to do conditional transformation or iterate over things.</p>
<p>The most common example is the <a href="https://en.wikipedia.org/wiki/C_preprocessor#Macro_definition_and_expansion" rel="nofollow noreferrer">C preprocessor</a>, which has a very limited but simple macro syntax.</p>
<h1>Contextful</h1>
<p>Languages that have contextful CT metaprogramming tend to have much richer support for inspecting the part of code that is being worked on.</p>
<p>There are multiple approaches to this choice: some languages (<a href="https://doc.rust-lang.org/book/ch19-06-macros.html" rel="nofollow noreferrer">Rust</a>, <a href="https://doc.rust-lang.org/book/ch19-06-macros.html#declarative-macros-with-macro_rules-for-general-metaprogramming" rel="nofollow noreferrer">Crystal</a>) add new syntax to the usual function definitions for working with compile-time code, others create a fully different system, others (<a href="https://docs.scala-lang.org/scala3/guides/macros/macros.html" rel="nofollow noreferrer">Scala 3</a>) use only normal syntax and specific types that represent parts of code.</p>
<h1>Workarounds</h1>
<p>Some uses of CT metaprogramming can be replaced by <a href="https://craftinginterpreters.com/representing-code.html#metaprogramming-the-trees" rel="nofollow noreferrer">programmatically generating source files</a> (which however requires adding a separate build step), and multiple e.g. C metaprogramming libraries exist.</p>
<h1>Closing remarks</h1>
<p>The &quot;macro&quot; name doesn't necessarily mean C-style &quot;dumb&quot; macros, and in fact is somewhat commonly used to refer to all types of compile type generation and modification of source code.</p>
<p>Your choices are the contextfulness (which drives many implementation details) and whether the syntax should be a superset of your normal code syntax or something special or just your normal syntax with special types (which probably can be decided a bit later).</p>

CC BY-SA 4.0
2023-05-18T09:20:20.727Z
2023-05-18T09:20:20.727Z
RubenVerg
CC BY-SA 4.0
2023-05-28T14:16:33.350Z
This is the best answer so far. It could perhaps be improved by mention the difference between hygenic and unhygenic macros.
Bruce Adams
<p>Interpreted languages can simply use string methods to piece together code, then evaluate them at runtime.</p>

CC BY-SA 4.0
2023-05-18T08:02:03.373Z
2023-05-18T08:02:03.373Z
Adám
# What alternatives to macros there is for meta programming on compile time?
Posted by: username_0 on 2023-05-18
For example instead of macros Zig has comptime feature that allows codegen like implementation of arbitrary arity functions while being native Zig code and not some separate macro language that isn't generally aware what given piece of code means in context of target language.
**Tags:** meta-programming
> **Comments:**
> **username_1**: Do you mean just code generation, or is e.g. reflection included in "meta-programming"? It would help to narrow this question down as it's quite broad.  
> **username_2**: Given that macros can be extremely wide as a concept, encompassing things like compile time execution, this is an ill-posed question. You can implement Zig style CT execution and call it a macro system.  
> **username_3**: Would this question be off topic if it was simply "what are methods to implement compile time meta-programming?"
We have many list possible design choices questions of that type already which seem to be on-topic  
---
## 4 answers
### Answer by username_4 on 2023-05-18. Score: 5
## Preprocessor/Parser Plugins

Instead of preprocessing your code, you just make your parser extensible with a plugin system. This system could allow plugins to convert custom syntax to AST of it's choice, or just convert to to different syntax for later stages of the parser to handle.

The advantage here is extremely flexibility. Instead of macros being limited to only function call syntax or using `!` identifier you can create completely new operators and syntactic constructs. You could even use it to disable some syntax you don't like or enforce linting rules.

Disadvantage is that it's less integrated with your source. Plugins would need to be built separately so your compiler can access them at compile time. It would also make it harder to learn your language if different projects can add different bits of syntax.

This is the approach used in the [Babel javascript transpiler.](https://babeljs.io/). It's also common in PHP where libraries overwrite the autoloader for similar effect. (though it's more like a preprocessor in that case)
> **Comments:**
> **username_5** (2023-05-18): This is the way Java and Kotlin (and other JVM langs I think) do it  
---
### Answer by username_6 on 2023-05-18. Score: 5
In Smalltalk, you do meta-programming by writing code that generates classes and methods. This is easy to do because classes are not syntactic but dynamic entities. Put differently, a class is not a piece of text but a data object in memory. To create a new class from Smalltalk code, you can for example send a message to class `Object` asking it to create a new subclass of itself. Next, you send a message to that new subclass asking it to compile a method into itself.
---
### Answer by username_7 on 2023-05-18. Score: 3
Compile-time metaprogramming generally can be split in two approaches:

# Context-less

Languages with context-less CT metaprogramming generally have some way to transform the source text of a file, but can't know what the code contains.

Often these don't even have ways (or [need complicated workarounds](https://stackoverflow.com/questions/6707148/foreach-macro-on-macros-arguments)) to do conditional transformation or iterate over things.

The most common example is the [C preprocessor](https://en.wikipedia.org/wiki/C_preprocessor#Macro_definition_and_expansion), which has a very limited but simple macro syntax.

# Contextful

Languages that have contextful CT metaprogramming tend to have much richer support for inspecting the part of code that is being worked on.

There are multiple approaches to this choice: some languages ([Rust](https://doc.rust-lang.org/book/ch19-06-macros.html), [Crystal](https://doc.rust-lang.org/book/ch19-06-macros.html#declarative-macros-with-macro_rules-for-general-metaprogramming)) add new syntax to the usual function definitions for working with compile-time code, others create a fully different system, others ([Scala 3](https://docs.scala-lang.org/scala3/guides/macros/macros.html)) use only normal syntax and specific types that represent parts of code.

# Workarounds

Some uses of CT metaprogramming can be replaced by [programmatically generating source files](https://craftinginterpreters.com/representing-code.html#metaprogramming-the-trees) (which however requires adding a separate build step), and multiple e.g. C metaprogramming libraries exist.

# Closing remarks

The "macro" name doesn't necessarily mean C-style "dumb" macros, and in fact is somewhat commonly used to refer to all types of compile type generation and modification of source code.

Your choices are the contextfulness (which drives many implementation details) and whether the syntax should be a superset of your normal code syntax or something special or just your normal syntax with special types (which probably can be decided a bit later).
> **Comments:**
> **username_3** (2023-05-28): This is the best answer so far. It could perhaps be improved by mention the difference between hygenic and unhygenic macros.  
---
### Answer by username_8 on 2023-05-18. Score: 1
Interpreted languages can simply use string methods to piece together code, then evaluate them at runtime.
---
