reflection based on my experience so far on compile-time meta-programming in D as a novice user: the problems

Simen Kjærås simen.kjaras at gmail.com
Mon Sep 14 12:27:34 UTC 2020


On Monday, 14 September 2020 at 10:54:07 UTC, Paul Backus wrote:
> Don't overthink it:
>
> string generate(string typeName)() {
>     return "%s bla;".format(typeName);
> }
>
> void main() {
>     mixin(generate!"int");
>     bla = 5;
> }

Even better:

mixin template generate(T) {
     T bla;
}

void main() {
     mixin generate!int;
     bla = 5;
}

String mixins, like inline assembly, is a very powerful tool. 
Just like inline assembly though, it's usually not the tool you 
should be reaching for. Generally, if it's possible to do 
something without string mixins, that way will be less error 
prone and more readable.

--
   Simen


More information about the Digitalmars-d mailing list