Metaprog can be abstruse
kdevel
kdevel at vogtner.de
Mon Jul 4 23:19:27 UTC 2022
On Monday, 4 July 2022 at 09:40:45 UTC, Bastiaan Veelo wrote:
> [...]
> Not necessarily.
> ```d
> immutable decimalRanks = iota(10).map!(p => 10.pow(p)).array;
> ```
Your example requires
```
import std.range; // for iota
import std.algorithm; // for map
import std.math; // for pow of int
```
Then your example is prone to errors:
```
immutable decimalRanks = iota(13).map!(p => 10.pow(p)).array; //
silent wrap around
```
(unit)test to the rescue:
```
auto make_decimal_rank ()
{
import std.range;
import std.algorithm;
import std.math;
return iota (13).map!(p => 10.pow (p)).array;
}
void main ()
{
import std.stdio;
immutable decimalRank = make_decimal_rank;
decimalRank.writeln;
typeof (decimalRank).stringof.writeln;
}
unittest {
enum ar13 = [
1,
10,
100,
1000,
10000,
100000,
1000000,
10000000,
100000000,
1000000000,
10000000000,
100000000000,
1000000000000
];
pragma (msg, typeof (ar13));
assert (make_decimal_rank == ar13);
}
```
Running:
```
dmd -checkaction=context -unittest -run decrank
long[]
decrank.d(35): [unittest] [1, 10, 100, 1000, 10000, 100000,
1000000, 10000000, 100000000, 1000000000, 1410065408, 1215752192,
-727379968] != [1, 10, 100, 1000, 10000, 100000, 1000000,
10000000, 100000000, 1000000000, 10000000000, 100000000000,
1000000000000]
1/1 modules FAILED unittests
```
More information about the Digitalmars-d
mailing list