Metaprog can be abstruse
Bastiaan Veelo
Bastiaan at Veelo.net
Mon Jul 4 09:40:45 UTC 2022
On Monday, 4 July 2022 at 08:24:01 UTC, user1234 wrote:
> Example:
>
> ```d
> auto genDecimalRanks()
> {
> import std.conv;
> auto r = 1;
> auto result = "[";
> foreach (i; 1 .. 11)
> {
> result ~= to!string(r);
> if (i < 10)
> result ~= ", ";
> r *= 10;
> }
> result ~= "]";
> return result;
> }
>
> /// like
> [1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000]
> immutable decimalRanks = mixin(genDecimalRanks);
> ```
>
> It takes much more space and time to write DDOC + the generator
> than the space + time required to write the equivalent explicit
> declaration without comments, i.e "self documenting".
>
> So metaprog is not the panacea, do you think to that before
> "meta-progrogramming", or do you "meta-prog" just because it's
> nice ?
Not necessarily.
```d
immutable decimalRanks = iota(10).map!(p => 10.pow(p)).array;
```
-- Bastiaan.
More information about the Digitalmars-d
mailing list