Metaprog can be abstruse
bauss
jj_1337 at live.dk
Mon Jul 4 10:29:24 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 ?
If you don't like the iota version then you can just do this:
(Modified version of yours.)
```d
auto genDecimalRanks()
{
import std.conv;
auto r = 1;
int[] result = [];
foreach (i; 1 .. 11)
{
result ~= r;
r *= 10;
}
return result;
}
/// like
[1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000]
static immutable decimalRanks = genDecimalRanks();
```
Everything at compile-time doesn't have to be strings and mixin :)
static and enum will trigger ctfe.
More information about the Digitalmars-d
mailing list