Mixin programming foreach

Dga123 Dga123 at 123.fi
Mon Sep 27 17:09:47 UTC 2021


On Monday, 27 September 2021 at 16:59:16 UTC, eXodiquas wrote:
> But my last question still stands, how do I build functions 
> that can work with those vectors because the type of those 
> vectors is created at compile time.

Here's another approach allowing to have a more obvious 
declaration name:

```d
import std;

string vectorBody(int dimension) {
   string result;
   foreach(d; 0..dimension)
     result ~= "int x" ~ d.to!string ~ " = 0;\n";
   return result;
}

struct Vector(int dimension)
{
     mixin(vectorBody(dimension));
}

alias Vector5 = Vector!5;
```



More information about the Digitalmars-d-learn mailing list