initializedArray
Philippe Sigaud
philippe.sigaud at gmail.com
Tue Dec 20 22:18:34 PST 2011
On Tue, Dec 20, 2011 at 23:42, Andrej Mitrovic
<andrej.mitrovich at gmail.com> wrote:
> 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
Ah OK. Where as your version does a deep dupping, I see.
>> 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..
I tend to do that also :)
> 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..
> }
Having thought a bit more about this, there is a difference between
static and dynamic arrays. For static arrays, the length is part of
the type, so getting them should be straightforward.
Untested:
size_t[] arrayLengths(A)(A a) if (isStaticArray!A)
{
static if (isStaticArray!(ElementType!A))
return arrayLengths(a[0]) ~ a.length;
else
return [a.length];
}
But for dynamic arrays, int[][] can be a jagged (non square) array.
And now that array literal are dynamic, the only way I see would be to
collect the max length for all dimensions.
[[1,2,3],
[4,5]] -> 2 rows, 3 columns.
>> 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?
There is 'is(A : B)', but when I tried this with your code, it didn't
work (maybe I was confused between double and floats). That's why I
used std.traits.isImplicitlyConvertible!(From,To)
More information about the Digitalmars-d
mailing list