digitalmars.D.learn - Array Copying syntax
- Antonio Corbi (12/12) Sep 16 2016 Hi!
- Adam D. Ruppe (4/6) Sep 16 2016 That works for all arrays. `s = t` for dynamically sized arrays
- Antonio Corbi (24/31) Sep 16 2016 Thank's Adam!
- Jonathan M Davis via Digitalmars-d-learn (16/18) Sep 16 2016 Well, I confess that I don't know why you would ever have thought that
- Antonio Corbi (11/32) Sep 16 2016 Hi Jonathan!
- Steven Schveighoffer (4/13) Sep 16 2016 Yeah, that's bad.
- Antonio Corbi (6/23) Sep 16 2016 Hi Steve,
- Steven Schveighoffer (4/27) Sep 16 2016 Hah, except it's actually wrong :) s = a compiles.
- Steven Schveighoffer (3/6) Sep 16 2016 Updated, should be good now.
- =?UTF-8?Q?Ali_=c3=87ehreli?= (4/10) Sep 16 2016 A short section of mine about that difference:
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
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
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: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?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
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
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: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! AntonioShouldn'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
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! AntonioYeah, that's bad. https://github.com/dlang/dlang.org/pull/1474 -Steve
Sep 16 2016
On Friday, 16 September 2016 at 18:12:22 UTC, Steven Schveighoffer wrote:On 9/16/16 2:03 PM, Antonio Corbi wrote:Hi Steve, Way clearer (at least for me) with your patch! Thanks, AntonioHi 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! AntonioYeah, that's bad. https://github.com/dlang/dlang.org/pull/1474 -Steve
Sep 16 2016
On 9/16/16 2:28 PM, Antonio Corbi wrote:On Friday, 16 September 2016 at 18:12:22 UTC, Steven Schveighoffer wrote:Hah, except it's actually wrong :) s = a compiles. I don't know what that line is supposed to mean now... -SteveOn 9/16/16 2:03 PM, Antonio Corbi wrote:Hi Steve, Way clearer (at least for me) with your patch!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! AntonioYeah, that's bad. https://github.com/dlang/dlang.org/pull/1474 -Steve
Sep 16 2016
On 9/16/16 2:48 PM, Steven Schveighoffer wrote:On 9/16/16 2:28 PM, Antonio Corbi wrote:Updated, should be good now. -SteveWay clearer (at least for me) with your patch!Hah, except it's actually wrong :) s = a compiles.
Sep 16 2016
On 09/16/2016 10:11 AM, Adam D. Ruppe wrote:On Friday, 16 September 2016 at 17:03:20 UTC, Antonio Corbi wrote:A short section of mine about that difference: http://ddili.org/ders/d.en/slices.html#ix_slices.assignment,%20array AliIs 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