www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Array Copying syntax

reply Antonio Corbi <amcb ggmail.com> writes:
Hi!

I was just playing with array initialization and copying and 
discovered that this syntax works as expected but it is not 
referenced under https://dlang.org/spec/arrays.html#array-copying:

-----8><-----
int[3] s;
int[3] t;

s = t;
-----8><-----

Is it safe to use or do I have to use the proposed 's[] = t;' or 
's[] = t[]' ?
Thank's!
Sep 16 2016
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Friday, 16 September 2016 at 17:03:20 UTC, Antonio Corbi wrote:
 Is it safe to use or do I have to use the proposed 's[] = t;' 
 or 's[] = t[]' ?
That works for all arrays. `s = t` for dynamically sized arrays (aka slices) just sets the references to the same, but for your statically sized arrays, it also copies.
Sep 16 2016
next sibling parent reply Antonio Corbi <amcb ggmail.com> writes:
On Friday, 16 September 2016 at 17:11:54 UTC, Adam D. Ruppe wrote:
 On Friday, 16 September 2016 at 17:03:20 UTC, Antonio Corbi 
 wrote:
 Is it safe to use or do I have to use the proposed 's[] = t;' 
 or 's[] = t[]' ?
That works for all arrays. `s = t` for dynamically sized arrays (aka slices) just sets the references to the same, but for your statically sized arrays, it also copies.
Thank's Adam! That's what my toy program told me: ---------------8><------------------ import std.stdio; alias matrix = uint[3][2]; int main(string[] args) { matrix s; matrix t = [[1,2,3], [4,5,6]]; writeln ("s= ", s); writeln ("s.ptr= " , s.ptr); writeln ("t= ", t); writeln ("t.ptr= " , t.ptr); writeln ("t[1][2]= ", t[1][2]); s = t; writeln ("s= ", s); writeln ("s.ptr= " , s.ptr); return 0; } ---------------8><------------------ ...but I wanted to be sure! :) Shouldn't it be mentioned then in the docs that this works for statically sized arrays and that in that case it copies contents?
Sep 16 2016
parent reply Jonathan M Davis via Digitalmars-d-learn writes:
On Friday, September 16, 2016 17:22:41 Antonio Corbi via Digitalmars-d-learn 
wrote:
 Shouldn't it be mentioned then in the docs that this works for
 statically sized arrays and that in that case it copies contents?
Well, I confess that I don't know why you would ever have thought that s = t; wouldn't work. The only mutable types that can't be assigned to like this are the ones that have disabled assignment or copying (which would only be certain structs). And I thought that the spec made it clear that static arrays were value types, in which case the behavior of s = t; should be obvious. So, I would have thought that it would be clear enough as-is, but clearly, it wasn't for you. So, I don't know. We do want the spec to be clear, but it would also be bad to add a bunch of redundant information in an attempt to be clearer. I'd have to go digging through the spec though to give any concrete suggestions on how to improve this though. - Jonathan M Davis
Sep 16 2016
parent reply Antonio Corbi <aaacorbi mail.com> writes:
On Friday, 16 September 2016 at 17:55:59 UTC, Jonathan M Davis 
wrote:
 On Friday, September 16, 2016 17:22:41 Antonio Corbi via 
 Digitalmars-d-learn wrote:
 Shouldn't it be mentioned then in the docs that this works for 
 statically sized arrays and that in that case it copies 
 contents?
Well, I confess that I don't know why you would ever have thought that s = t; wouldn't work. The only mutable types that can't be assigned to like this are the ones that have disabled assignment or copying (which would only be certain structs). And I thought that the spec made it clear that static arrays were value types, in which case the behavior of s = t; should be obvious. So, I would have thought that it would be clear enough as-is, but clearly, it wasn't for you. So, I don't know. We do want the spec to be clear, but it would also be bad to add a bunch of redundant information in an attempt to be clearer. I'd have to go digging through the spec though to give any concrete suggestions on how to improve this though. - Jonathan M Davis
Hi Jonathan! Probably this entry in https://dlang.org/spec/arrays.html#usage confused me a bit. int[3] s; ... s = ...; // error, since s is a compiled in static // reference to an array. Thanks for your help! Antonio
Sep 16 2016
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 9/16/16 2:03 PM, Antonio Corbi wrote:

 Hi Jonathan!

 Probably this entry in https://dlang.org/spec/arrays.html#usage confused
 me a bit.

 int[3] s;
 ....
 s = ...;   // error, since s is a compiled in static
            // reference to an array.

 Thanks for your help!
 Antonio
Yeah, that's bad. https://github.com/dlang/dlang.org/pull/1474 -Steve
Sep 16 2016
parent reply Antonio Corbi <aaacorbi mail.com> writes:
On Friday, 16 September 2016 at 18:12:22 UTC, Steven 
Schveighoffer wrote:
 On 9/16/16 2:03 PM, Antonio Corbi wrote:

 Hi Jonathan!

 Probably this entry in 
 https://dlang.org/spec/arrays.html#usage confused
 me a bit.

 int[3] s;
 ....
 s = ...;   // error, since s is a compiled in static
            // reference to an array.

 Thanks for your help!
 Antonio
Yeah, that's bad. https://github.com/dlang/dlang.org/pull/1474 -Steve
Hi Steve, Way clearer (at least for me) with your patch! Thanks, Antonio
Sep 16 2016
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 9/16/16 2:28 PM, Antonio Corbi wrote:
 On Friday, 16 September 2016 at 18:12:22 UTC, Steven Schveighoffer wrote:
 On 9/16/16 2:03 PM, Antonio Corbi wrote:

 Hi Jonathan!

 Probably this entry in https://dlang.org/spec/arrays.html#usage confused
 me a bit.

 int[3] s;
 ....
 s = ...;   // error, since s is a compiled in static
            // reference to an array.

 Thanks for your help!
 Antonio
Yeah, that's bad. https://github.com/dlang/dlang.org/pull/1474 -Steve
Hi Steve, Way clearer (at least for me) with your patch!
Hah, except it's actually wrong :) s = a compiles. I don't know what that line is supposed to mean now... -Steve
Sep 16 2016
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 9/16/16 2:48 PM, Steven Schveighoffer wrote:
 On 9/16/16 2:28 PM, Antonio Corbi wrote:
 Way clearer (at least for me) with your patch!
Hah, except it's actually wrong :) s = a compiles.
Updated, should be good now. -Steve
Sep 16 2016
prev sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 09/16/2016 10:11 AM, Adam D. Ruppe wrote:
 On Friday, 16 September 2016 at 17:03:20 UTC, Antonio Corbi wrote:
 Is it safe to use or do I have to use the proposed 's[] = t;' or 's[]
 = t[]' ?
That works for all arrays. `s = t` for dynamically sized arrays (aka slices) just sets the references to the same, but for your statically sized arrays, it also copies.
A short section of mine about that difference: http://ddili.org/ders/d.en/slices.html#ix_slices.assignment,%20array Ali
Sep 16 2016