<p>In Rust:</p>
<pre class="lang-rust prettyprint-override"><code>#[derive(Display)]
struct Foo&lt;T&gt;(T);
</code></pre>
<p>If <code>T</code> conforms to the <code>Display</code> trait, then <code>Foo&lt;T&gt;</code> can automatically implement <code>Display</code> using a macro.</p>
<p>Is there an approach to accomplish this automatic implementation that doesn't rely on the macro system?</p>

<rust><traits><macro><typeclasses>
Implementing automatic derivation without macros?
CC BY-SA 4.0
2023-12-01T09:20:41.007Z
In C# attributes and reflection can be used to automatically implement behavior: `[DebuggerDisplay("x = {x} y = {y}")] class Foo`. It's not an interface though. [DebuggerDisplay](https://learn.microsoft.com/en-us/visualstudio/debugger/using-the-debuggerdisplay-attribute?view=vs-2022)
Eldritch Conundrum
CC BY-SA 4.0
2023-12-01T13:02:19.170Z
Either reflection (basically, resolve the structure and implementation at runtime instead of compile time), or structural type-level hackery like Haskell's GHC generics (https://wiki.haskell.org/GHC.Generics)
tarzh
CC BY-SA 4.0
2023-12-01T15:51:15.477Z
Or compile-time execution (comptime) like in Zig (https://kristoff.it/blog/what-is-zig-comptime/)
tarzh
<h3>Compile time source generation</h3>
<p>That is, instead of using simple string concatenation (via a crude macro system) or trying to make the macro system <a href="https://stackoverflow.com/questions/3136686/is-the-c99-preprocessor-turing-complete">Turing</a> <a href="https://sdleffler.github.io/RustTypeSystemTuringComplete/" rel="nofollow noreferrer">complete</a> with a lot of extra syntax, you could use the very source language to do this.</p>
<p>Something like <a href="https://learn.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/source-generators-overview" rel="nofollow noreferrer">C# source generators</a>.</p>
<p>This is a extreme form of the <a href="https://en.wikipedia.org/wiki/Lexer_hack" rel="nofollow noreferrer">lexer hack</a>, where the compiler runs some previous compiled source generator at some specifics steps on parsing and/or compiling stages of normal code, so this source generators inject pure new code, or directly inspect and create/modify AST nodes in the intermediary compiled state.</p>
<pre class="lang-rust prettyprint-override"><code>#[sgen(Display)]   // Register a source generator for later execution.
struct Foo&lt;T&gt;(T);  // The &quot;visible&quot; source code, parsed from file.

// When the compiler finds the end of struct in visible source code,
// execute registered source generators, to inject new code or to
// modify the parsed AST. (after the only `;` in this example)
</code></pre>
<p>If the compiler is already <a href="https://en.wikipedia.org/wiki/Bootstrapping_(compilers)" rel="nofollow noreferrer">bootstrapped</a>, it's easier to implement source generators, as any internal compiler infrastructure can be exposed without code duplication.</p>

CC BY-SA 4.0
2023-12-01T15:41:50.973Z
2023-12-01T21:33:21.293Z
2023-12-01T21:33:21.293Z
André L F S Bacci
CC BY-SA 4.0
2023-12-01T21:42:20.813Z
Another example is Swift's [Sourcery](https://krzysztofzablocki.github.io/Sourcery/) and Kotlin's [KSP](https://kotlinlang.org/docs/ksp-overview.html).
tarzh
# Implementing automatic derivation without macros?
Posted by: username_0 on 2023-12-01
In Rust:
#[derive(Display)]
struct Foo(T);

If T conforms to the Display trait, then Foo can automatically implement Display using a macro.
Is there an approach to accomplish this automatic implementation that doesn't rely on the macro system?
**Tags:** rust traits macro typeclasses
> **Comments:**
> **username_1**: In C# attributes and reflection can be used to automatically implement behavior: `[DebuggerDisplay("x = {x} y = {y}")] class Foo`. It's not an interface though. [DebuggerDisplay](https://learn.microsoft.com/en-us/visualstudio/debugger/using-the-debuggerdisplay-attribute?view=vs-2022)  
> **username_2**: Either reflection (basically, resolve the structure and implementation at runtime instead of compile time), or structural type-level hackery like Haskell's GHC generics (https://wiki.haskell.org/GHC.Generics)  
> **username_2**: Or compile-time execution (comptime) like in Zig (https://kristoff.it/blog/what-is-zig-comptime/)  
---
## 1 answers
### Answer by username_3 on 2023-12-01. Score: 4
Compile time source generation
That is, instead of using simple string concatenation (via a crude macro system) or trying to make the macro system Turing complete with a lot of extra syntax, you could use the very source language to do this.
Something like C# source generators.
This is a extreme form of the lexer hack, where the compiler runs some previous compiled source generator at some specifics steps on parsing and/or compiling stages of normal code, so this source generators inject pure new code, or directly inspect and create/modify AST nodes in the intermediary compiled state.
#[sgen(Display)]   // Register a source generator for later execution.
struct Foo(T);  // The "visible" source code, parsed from file.

// When the compiler finds the end of struct in visible source code,
// execute registered source generators, to inject new code or to
// modify the parsed AST. (after the only `;` in this example)

If the compiler is already bootstrapped, it's easier to implement source generators, as any internal compiler infrastructure can be exposed without code duplication.
> **Comments:**
> **username_2** (2023-12-01): Another example is Swift's [Sourcery](https://krzysztofzablocki.github.io/Sourcery/) and Kotlin's [KSP](https://kotlinlang.org/docs/ksp-overview.html).  
---
