Andrei Alexandrescu:
So I'm looking at creation functions and in particular creation
functions for arrays.
1. Follow the new int[n] convention:
auto a = allok.make!(int[])(42);
assert(a.length == 42);
assert(a.equal(repeat(0, 42));
2. Follow the [ literal ] convention:
auto a = allok.make!(int[])(42);
assert(a.length == 1);
assert(a[0] == 42);
Both cases are needed. Unfortunately we don't have named
arguments in D, otherwise you can use the same name for both
operations, using different arguments.
Also keep in mind that in std.array we have functions like
assocArray, uninitializedArray, and minimallyInitializedArray.
I suggest to take a look at a similar function in Scala and
especially in Lisp. Lisp has decades of experience to steal from.
A common desire is to create an array based on a given function
of its indexes:
immutable a = allok.make!(int[][])(tuple(2, 3), (r, c) => r * 10
+ c);
==>
[[0, 1, 2], [10, 11, 12]]
This is rather handy when you want to create immutable arrays.
auto a = allok.make!(int[])(42, uninitialized(5), 44);
From my experience this is a very uncommon situation, I don't
remember if I have ever had such need. (In most cases you want an
initialized or an unitialized array. More rarely you want to
initialize the first N items and leave M items not initialized).
Bye,
bearophile