Array literals are weird.

norm norm.rowtree at gmail.com
Tue May 4 01:59:48 UTC 2021


On Sunday, 2 May 2021 at 08:07:58 UTC, Imperatorn wrote:
> On Saturday, 1 May 2021 at 19:49:51 UTC, russhy wrote:
>> On Saturday, 1 May 2021 at 12:22:50 UTC, evilrat wrote:
>>> On Saturday, 1 May 2021 at 11:50:27 UTC, Adam D. Ruppe wrote:
>>>>[...]
>>>
>>> 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);
>>> }
>>> ```
>>
>>
>> don't you realize something is weird?
>>
>> someone is asking to be able to do auto a = [1, 2, 3]
>>
>> and you propose a template?
>>
>> this is asking people to look for alternative language
>>
>> auto a = new[1,2,3]  <== this should be allocated array
>> auto a = [1,2,3] <== this should be static array
>>
>> let's fix that, shall we? instead of telling people to bloat 
>> their files with templates
>
> Is there any way to enable this in the language?
>
> ```d
> auto a = [1,2,3] + [4,5,6]; //[5,7,9]
> ```
>
> I want to allow the operation.
>
> If I made a dmd fork, could I change this behavior "easily" or 
> would it require a blood sacrifice

I think there was once a PR that made this nice and easy:

https://github.com/dlang/dmd/pull/3615

```d
auto a[$] = [1,2,3];
```

I was disappointed it never got accepted but I am sure there were 
good reasons. At the time I think it was "this should be a 
library solution" and that was probably where `staticArray` came 
from








More information about the Digitalmars-d mailing list