Thread overview
[Issue 8280] std.array.uninitializedArrayExtend, std.array.initializedArray
Jun 13, 2014
safety0ff.bugz
Dec 17, 2022
Iain Buclaw
June 13, 2014
https://issues.dlang.org/show_bug.cgi?id=8280

bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|std.array.uninitializedArra |std.array.uninitializedArra
                   |yExtend                     |yExtend,
                   |                            |std.array.initializedArray

--- Comment #1 from bearophile_hugs@eml.cc ---
I also suggest to add a "std.array.initializedArray" function similar to std.array.uninitializedArray, that accepts another extra argument that is the initialization value or a lambda to fill the array:

This allocated an array of 100 spaces:

auto data = initializedArray!(char[])(100, ' ');

It is equivalent to:

auto data = uninitializedArray!(char[])(100);
data[] = ' ';


Another example usage (note the result is immutable):

immutable data = initializedArray!(int[])(50, i => i);

That is equivalent to (but there is no data_ temporary variable):

auto data_ = uninitializedArray!(int[])(50);
foreach (immutable i; 0 .. 50)
    data_[i] = i;
immutable data = data_.assumeUnique;


Another example usage:

immutable mat = initializedArray!(int[][])(20, 20, (i, j) => i * j);

That is equivalent to (but there is no mat_ temporary variable):

auto mat_ = uninitializedArray!(int[][])(20, 20);
foreach (immutable i; 0 .. 20)
    foreach (immutable j; 0 .. 20)
        mat_[i][j] = i * j;
immutable mat = mat_.assumeUnique;

--
June 13, 2014
https://issues.dlang.org/show_bug.cgi?id=8280

safety0ff.bugz <safety0ff.bugz@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |safety0ff.bugz@gmail.com

--- Comment #2 from safety0ff.bugz <safety0ff.bugz@gmail.com> ---
See also: Issue #12444

--
June 13, 2014
https://issues.dlang.org/show_bug.cgi?id=8280

--- Comment #3 from bearophile_hugs@eml.cc ---
(In reply to bearophile_hugs from comment #1)

> auto data = initializedArray!(char[])(100, ' ');
> 
> It is equivalent to:
> 
> auto data = uninitializedArray!(char[])(100);
> data[] = ' ';
> 
> 
> Another example usage (note the result is immutable):
> 
> immutable data = initializedArray!(int[])(50, i => i);

Perhaps there is one case where there is some ambiguity:

auto funcs = initializedArray!(int function(int)[])(10, i => i);

--
June 13, 2014
https://issues.dlang.org/show_bug.cgi?id=8280

--- Comment #4 from bearophile_hugs@eml.cc ---
(In reply to bearophile_hugs from comment #3)

> auto funcs = initializedArray!(int function(int)[])(10, i => i);

There can be some very uncommon strange cases, but that's a value and not a lambda to fill the array.

--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=8280

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P2                          |P4

--