initializedArray
Andrej Mitrovic
andrej.mitrovich at gmail.com
Tue Dec 20 14:42:27 PST 2011
On 12/20/11, Philippe Sigaud <philippe.sigaud at gmail.com> wrote:
> 1) What's the difference with using auto arr2 == [[1,2],[3,4]].dup; ?
> (I honestly asks, I don't know much about D's assignements)
dup is not a deep copy, it only copies the first elements. The first
elements are two slices, so:
void main()
{
auto a = [[1, 2], [3, 4]];
auto b = a.dup;
a[0][0] = 10;
assert(b[] == [[1, 2], [3, 4]]); // fails, b[0][0] is 10
}
> 2) You can get the lengths of [[1,2],[3,4]], so the 2,2 args is
> redundant. What happens if you type:
>
> auto arr2 = initializedArray!(int[][])([[1, 2], [3, 4]], 4, 10);
Right, it's broken to say the least. That's what I get for a 10 second
implementation.. Btw, uninitializedArray takes variadic arguments, how
would I get the lengths of the dimensions as a tuple that can be
passed in place of the variadic argument? I.e.:
auto dupArray(int[][] src) // say src is int[1][2]
{
auto arr = uninitializedArray!(int[][])( /* need 1, 2 here */ );
// ..then copy each element..
}
I could use a mixin, but there should be an easier way to do this?
You're good with templates so I'm asking you! :)
> 3) I still think you should relax the constraint on the init value's
> type. You do not need it to be *exactly* BaseElementType!T. Thats
> stops you from writing
>
> auto arr2 = initializedArray!(float[][])(3, 2,3);
I often fall to this trap. I think there's a syntax for checking if a
type is implicitly convertible to another type? I haven't read your
book fully yet, btw.
> 4-ish) No need to attribute the rank/BaseElementType code to me :-)
>
I say the same thing for when people use my code, but usually it's
best not to assume ownership. :)
More information about the Digitalmars-d
mailing list