-
-
Notifications
You must be signed in to change notification settings - Fork 705
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
std.array.uninitializedArrayExtend, std.array.initializedArray #9928
Labels
Comments
bearophile_hugs commented on 2014-06-13T20:10:18ZI 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; |
safety0ff.bugz commented on 2014-06-13T20:13:57ZSee also: Issue #12444 |
bearophile_hugs commented on 2014-06-13T20:45:47Z(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); |
bearophile_hugs commented on 2014-06-13T20:48:19Z(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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
bearophile_hugs reported this on 2012-06-21T17:19:31Z
Transfered from https://issues.dlang.org/show_bug.cgi?id=8280
CC List
Description
The text was updated successfully, but these errors were encountered: