Using Enums to Generate Scoped List of Names
Salih Dincer
salihdb at hotmail.com
Sat Aug 20 11:14:54 UTC 2022
On Tuesday, 16 August 2022 at 15:34:30 UTC, Ali Çehreli wrote:
> On 8/16/22 08:07, Walter Bright wrote:
> And your array-at-compile-time example can subjectively be
> better like this:
>```d
> import std;
>
> void main() {
> pragma(msg, iota(20).map!(n => n * n).array);
> }
>```
Generates different types if not specifically specified. Because
Walter initialized a static array. Although your example runs
with #pragma , it creates a dynamic array:
```d
import std.stdio;
enum N = 10;
void main()
{
int[N] squares_A = () {
int[N] squares;
foreach (i; 0 .. N)
squares[i] = i * i;
return squares;
}();
squares_A.writeln(": ", typeid(squares_A));
import std.algorithm, std.range;
//pragma(msg, iota(20).map!(n => n * n).array);/*
auto squares_B = iota(N).map!(n => n * n).array;
squares_B ~= 100;
squares_B.writeln(": ", typeid(squares_B));//*/
/* [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]: int[10]
* [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]: int[]
*/
}
```
Also, squares_A doesn't get any help from any modules.
SDB at 79
More information about the Digitalmars-d
mailing list