Array literals are weird.

evilrat evilrat666 at gmail.com
Sat May 1 12:22:50 UTC 2021


On Saturday, 1 May 2021 at 11:50:27 UTC, Adam D. Ruppe wrote:
>
> or you can use the library `.staticArray` thing to expressly 
> indicate your intention on the original foo
>
> foo([1,2,3].staticArray);
>
>

Is there one in Phobos? Anyway this works, but maybe it can be 
made more type safe with constraints or just with improved symtax.

```d
// compiles with -vgc and -betterC (weird, also without import 
stdc.core.stdio)
import std.range : ElementType;

template staticArray(alias T)
{
	enum ElementType!(typeof(T))[T.length] staticArray = T;
}

extern(C) void main()
{
	import core.stdc.stdio;
	auto arr = staticArray!([1,2,3]);
	pragma(msg, typeof(arr)); // int[3]
	foreach(i; arr)
		printf("%d\n", i);
}
```


More information about the Digitalmars-d mailing list