[Issue 6421] Require initialization of static arrays with array literals not to allocate

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sun May 4 04:06:45 PDT 2014


https://issues.dlang.org/show_bug.cgi?id=6421

--- Comment #8 from Andrej Mitrovic <andrej.mitrovich at gmail.com> ---
(In reply to Kenji Hara from comment #7)
> But "length inference" on variable declaration is a useful syntax.
> 
> float[$] arr = [1, 2, 3];          // typeof(arr) == float[3]
> auto[$] arr = [1.0f, 2.0f, 3.0f];  // ditto

What do you think about my extension to the new type construction syntax?:

float[3] arr = float[3]([1, 2, 3]);

I'm thinking it could be a more generic solution (more composable in
template/generic code) since you could do things like:

-----
float[3] arr;
arr = float[3]([1, 2, 3]);
-----

-----
float[3] arr;
arr = float[arr.length]([1, 2, 3]);
-----

-----
float[3] arr;
arr = typeof(arr)([1, 2, 3]);
-----

-----
int[] arr;
arr.length = 3;
arr[] += int[3]([1, 2, 3];
arr[] += int[3]([1, 2, 3];
assert(arr == [2, 4, 6]);
-----

-----
void foo(Arr)(ref Arr arr) if ( isStaticArray!Arr) { }
void foo(Arr)(Arr arr)     if (!isStaticArray!Arr) { }
foo(int[2]([1, 2]));  // explicitly pick overload
-----

And things like that.

--


More information about the Digitalmars-d-bugs mailing list