## digitalmars.D - Array slice assign syntax

- bearophile <bearophileHUGS lycos.com> Nov 15 2011
- Manu <turkeyman gmail.com> Nov 16 2011
- "Marco Leise" <Marco.Leise gmx.de> Nov 16 2011
- =?utf-8?Q?Simen_Kj=C3=A6r=C3=A5s?= <simen.kjaras gmail.com> Nov 16 2011
- "Regan Heath" <regan netmail.co.nz> Nov 16 2011
- Timon Gehr <timon.gehr gmx.ch> Nov 16 2011
- "Martin Nowak" <dawg dawgfoto.de> Nov 17 2011
- Timon Gehr <timon.gehr gmx.ch> Nov 17 2011

The Bugzilla issues that I really care about is a not an useless long list, it's about fifteen items long, and this post is about one of them. I think current array slice assign syntax is a bit messy, and I think this should be addressed. Kenji Hara has recently written a very nice program (that here I have modified a bit for clarity) that shows the current array assign situation: import std.stdio, std.typetuple; void main() { writeln("Rhs is an array, is it compilable?"); writeln("a\t/ b\t\ta=b\ta[]=b\ta=b[]\ta[]=b[]"); foreach (i, Lhs; TypeTuple!(int[3], int[])) foreach (j, Rhs; TypeTuple!(int[3], int[])) { writef("%s\t/ %s ", Lhs.stringof, Rhs.stringof); Lhs a = [0,0,0]; Rhs b = [1,2,3]; writef("\t%s", __traits(compiles, { a = b; })); writef("\t%s", __traits(compiles, { a[] = b; })); writef("\t%s", __traits(compiles, { a = b[]; })); writef("\t%s", __traits(compiles, { a[] = b[]; })); writeln(); } writeln("\nRhs is a element, is it compilable?"); writeln("a\t\t\ta=N\ta[]=N\ta[0..2]=N"); foreach (Lhs; TypeTuple!(int[3], int[])) { writef("%s\t\t", Lhs.stringof); Lhs a = [0,0,0]; writef("\t%s", __traits(compiles, { a = 9; })); writef("\t%s", __traits(compiles, { a[] = 9; })); writef("\t%s", __traits(compiles, { a[0..2] = 9; })); writeln(); } } Currently (DMD 2.057head, despite I think Walter has not updated DMD version number yet) it prints: Rhs is an array, is it compilable? a / b a=b a[]=b a=b[] a[]=b[] int[3u] / int[3u] true true true true int[3u] / int[] true true true true int[] / int[3u] true true true true int[] / int[] true true true true Rhs is a element, is it compilable? a a=N a[]=N a[0..2]=N int[3u] true true true int[] false true true This also means this is currently accepted: void main() { int[3] a; a = 1; assert(a == [1, 1, 1]); } While this is not accepted: void main() { int[] b = new int[3]; b = 1; assert(b == [1, 1, 1]); //Error: cannot implicitly convert expression (1) of type int to int[] } I'd like D to require a[]=1 in that first case too. I'd like the [] to be required every time an O(n) vector operation is done, for: - constancy with all other vector operations among two arrays, that require []; - and to avoid unwanted (and not easy to spot in the code) O(n) operations; - bugs and confusion in D newbies that don't have memorized all current special cases. On the other hand Don says that [] is only required for lvalues. I think this boils to a new table like this: Rhs is an array, is it compilable? a / b a=b a[]=b a=b[] a[]=b[] int[3u] / int[3u] FALSE true FALSE true int[3u] / int[] FALSE true FALSE true int[] / int[3u] FALSE true FALSE true int[] / int[] true true true true Rhs is a element, is it compilable? a a=N a[]=N a[0..2]=N int[3u] FALSE true true int[] false true true Now if there's a [] on the left, then it's an O(n) vector operation (like a copy), otherwise it's O(1). That also means: void main() { int[] a = new int[3]; int[] b = new int[3]; a = b; // OK, copies just array fat reference } void main() { int[3] a, b; a = b; // Not OK, hidden vector op } I am not sure this new table is fully correct, but it's a start. Fixes of mistakes are welcomes. ----------------------- This is an alternative proposal. On the other hand this vector op syntax doesn't currently compile: void main() { int[3] a, b; a[] += b; } So if array assign is seen as a normal vector op, then the [] is needed on the right too: Rhs is an array, is it compilable? a / b a=b a[]=b a=b[] a[]=b[] int[3u] / int[3u] FALSE FALSE FALSE true int[3u] / int[] FALSE FALSE FALSE true int[] / int[3u] FALSE FALSE FALSE true int[] / int[] true FALSE FALSE true Rhs is a element, is it compilable? a a=N a[]=N a[0..2]=N int[3u] FALSE true true int[] false true true Where the two cases with dynamic arrays are syntax errors to keep more symmetry: void main() { int[] a = new int[3]; int[] b = new int[3]; a[] = b; // error a = b[]; // error } ----------------------- Lot of time ago I have opened bug issue 3971 on this topic, but that Bugzilla thread contains various mistakes and some parts of it are obsolete. Bye, bearophile

Nov 15 2011

--0016364d3101e3545504b1d5c9b2 Content-Type: text/plain; charset=UTF-8 This makes perfect sense to me as a new-comer, and having not had much experience writing this sort of code, this is exactly how I would have assumed it is now. I would have been in for a confusing surprise had I tried to do any of this stuff. On 16 November 2011 05:24, bearophile <bearophileHUGS lycos.com> wrote:The Bugzilla issues that I really care about is a not an useless long list, it's about fifteen items long, and this post is about one of them. I think current array slice assign syntax is a bit messy, and I think this should be addressed. Kenji Hara has recently written a very nice program (that here I have modified a bit for clarity) that shows the current array assign situation: import std.stdio, std.typetuple; void main() { writeln("Rhs is an array, is it compilable?"); writeln("a\t/ b\t\ta=b\ta[]=b\ta=b[]\ta[]=b[]"); foreach (i, Lhs; TypeTuple!(int[3], int[])) foreach (j, Rhs; TypeTuple!(int[3], int[])) { writef("%s\t/ %s ", Lhs.stringof, Rhs.stringof); Lhs a = [0,0,0]; Rhs b = [1,2,3]; writef("\t%s", __traits(compiles, { a = b; })); writef("\t%s", __traits(compiles, { a[] = b; })); writef("\t%s", __traits(compiles, { a = b[]; })); writef("\t%s", __traits(compiles, { a[] = b[]; })); writeln(); } writeln("\nRhs is a element, is it compilable?"); writeln("a\t\t\ta=N\ta[]=N\ta[0..2]=N"); foreach (Lhs; TypeTuple!(int[3], int[])) { writef("%s\t\t", Lhs.stringof); Lhs a = [0,0,0]; writef("\t%s", __traits(compiles, { a = 9; })); writef("\t%s", __traits(compiles, { a[] = 9; })); writef("\t%s", __traits(compiles, { a[0..2] = 9; })); writeln(); } } Currently (DMD 2.057head, despite I think Walter has not updated DMD version number yet) it prints: Rhs is an array, is it compilable? a / b a=b a[]=b a=b[] a[]=b[] int[3u] / int[3u] true true true true int[3u] / int[] true true true true int[] / int[3u] true true true true int[] / int[] true true true true Rhs is a element, is it compilable? a a=N a[]=N a[0..2]=N int[3u] true true true int[] false true true This also means this is currently accepted: void main() { int[3] a; a = 1; assert(a == [1, 1, 1]); } While this is not accepted: void main() { int[] b = new int[3]; b = 1; assert(b == [1, 1, 1]); //Error: cannot implicitly convert expression (1) of type int to int[] } I'd like D to require a[]=1 in that first case too. I'd like the [] to be required every time an O(n) vector operation is done, for: - constancy with all other vector operations among two arrays, that require []; - and to avoid unwanted (and not easy to spot in the code) O(n) operations; - bugs and confusion in D newbies that don't have memorized all current special cases. On the other hand Don says that [] is only required for lvalues. I think this boils to a new table like this: Rhs is an array, is it compilable? a / b a=b a[]=b a=b[] a[]=b[] int[3u] / int[3u] FALSE true FALSE true int[3u] / int[] FALSE true FALSE true int[] / int[3u] FALSE true FALSE true int[] / int[] true true true true Rhs is a element, is it compilable? a a=N a[]=N a[0..2]=N int[3u] FALSE true true int[] false true true Now if there's a [] on the left, then it's an O(n) vector operation (like a copy), otherwise it's O(1). That also means: void main() { int[] a = new int[3]; int[] b = new int[3]; a = b; // OK, copies just array fat reference } void main() { int[3] a, b; a = b; // Not OK, hidden vector op } I am not sure this new table is fully correct, but it's a start. Fixes of mistakes are welcomes. ----------------------- This is an alternative proposal. On the other hand this vector op syntax doesn't currently compile: void main() { int[3] a, b; a[] += b; } So if array assign is seen as a normal vector op, then the [] is needed on the right too: Rhs is an array, is it compilable? a / b a=b a[]=b a=b[] a[]=b[] int[3u] / int[3u] FALSE FALSE FALSE true int[3u] / int[] FALSE FALSE FALSE true int[] / int[3u] FALSE FALSE FALSE true int[] / int[] true FALSE FALSE true Rhs is a element, is it compilable? a a=N a[]=N a[0..2]=N int[3u] FALSE true true int[] false true true Where the two cases with dynamic arrays are syntax errors to keep more symmetry: void main() { int[] a = new int[3]; int[] b = new int[3]; a[] = b; // error a = b[]; // error } ----------------------- Lot of time ago I have opened bug issue 3971 on this topic, but that Bugzilla thread contains various mistakes and some parts of it are obsolete. Bye, bearophile

--0016364d3101e3545504b1d5c9b2 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable This makes perfect sense to me as a new-comer, and having not had much expe= rience writing this sort of code, this is exactly how I would have assumed = it is now.<div>I would have been in for a confusing surprise had I tried to= do any of this stuff.<br> <br><div class=3D"gmail_quote">On 16 November 2011 05:24, bearophile <span = dir=3D"ltr"><<a href=3D"mailto:bearophileHUGS lycos.com" target=3D"_blan= k">bearophileHUGS lycos.com</a>></span> wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex">The Bugzilla issues that I really care about= is a not an useless long list, it's about fifteen items long, and this= post is about one of them.<br> <br> I think current array slice assign syntax is a bit messy, and I think this = should be addressed.<br> <br> Kenji Hara has recently written a very nice program (that here I have modif= ied a bit for clarity) that shows the current array assign situation:<br> <br> <br> import std.stdio, std.typetuple;<br> <br> void main() {<br> =C2=A0 =C2=A0writeln("Rhs is an array, is it compilable?");<br> =C2=A0 =C2=A0writeln("a\t/ b\t\ta=3Db\ta[]=3Db\ta=3Db[]\ta[]=3Db[]&qu= ot;);<br> <br> =C2=A0 =C2=A0foreach (i, Lhs; TypeTuple!(int[3], int[]))<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0foreach (j, Rhs; TypeTuple!(int[3], int[])) {<b= r> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0writef("%s\t/ %s =C2=A0"= ;, Lhs.stringof, Rhs.stringof);<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Lhs a =3D [0,0,0];<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Rhs b =3D [1,2,3];<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0writef("\t%s", __traits= (compiles, { a =C2=A0 =3D b; =C2=A0 }));<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0writef("\t%s", __traits= (compiles, { a[] =3D b; =C2=A0 }));<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0writef("\t%s", __traits= (compiles, { a =C2=A0 =3D b[]; }));<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0writef("\t%s", __traits= (compiles, { a[] =3D b[]; }));<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0writeln();<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0}<br> <br> <br> =C2=A0 =C2=A0writeln("\nRhs is a element, is it compilable?");<b= r> =C2=A0 =C2=A0writeln("a\t\t\ta=3DN\ta[]=3DN\ta[0..2]=3DN");<br> <br> =C2=A0 =C2=A0foreach (Lhs; TypeTuple!(int[3], int[])) {<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0writef("%s\t\t", Lhs.stringof);<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0Lhs a =3D [0,0,0];<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0writef("\t%s", __traits(compiles, { a= =C2=A0 =C2=A0 =C2=A0 =3D 9; }));<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0writef("\t%s", __traits(compiles, { a= [] =C2=A0 =C2=A0 =3D 9; }));<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0writef("\t%s", __traits(compiles, { a= [0..2] =3D 9; }));<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0writeln();<br> =C2=A0 =C2=A0}<br> }<br> <br> <br> <br> Currently (DMD 2.057head, despite I think Walter has not updated DMD versio= n number yet) it prints:<br> <br> <br> Rhs is an array, is it compilable?<br> a =C2=A0 =C2=A0 =C2=A0 / b =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 a=3Db = =C2=A0 =C2=A0 a[]=3Db =C2=A0 a=3Db[] =C2=A0 a[]=3Db[]<br> int[3u] / int[3u] =C2=A0 =C2=A0 =C2=A0 true =C2=A0 =C2=A0true =C2=A0 =C2=A0= true =C2=A0 =C2=A0true<br> int[3u] / int[] =C2=A0 =C2=A0 =C2=A0 =C2=A0 true =C2=A0 =C2=A0true =C2=A0 = =C2=A0true =C2=A0 =C2=A0true<br> int[] =C2=A0 / int[3u] =C2=A0 =C2=A0 =C2=A0 true =C2=A0 =C2=A0true =C2=A0 = =C2=A0true =C2=A0 =C2=A0true<br> int[] =C2=A0 / int[] =C2=A0 =C2=A0 =C2=A0 =C2=A0 true =C2=A0 =C2=A0true =C2= =A0 =C2=A0true =C2=A0 =C2=A0true<br> <br> Rhs is a element, is it compilable?<br> a =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 a=3DN =C2=A0 =C2=A0 a[]=3DN =C2=A0 a[0..2]=3DN<br> int[3u] =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 true =C2=A0= =C2=A0true =C2=A0 =C2=A0true<br> int[] =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 false = =C2=A0 true =C2=A0 =C2=A0true<br> <br> <br> <br> This also means this is currently accepted:<br> <br> void main() {<br> =C2=A0 =C2=A0int[3] a;<br> =C2=A0 =C2=A0a =3D 1;<br> =C2=A0 =C2=A0assert(a =3D=3D [1, 1, 1]);<br> }<br> <br> <br> While this is not accepted:<br> <br> void main() {<br> =C2=A0 =C2=A0int[] b =3D new int[3];<br> =C2=A0 =C2=A0b =3D 1;<br> =C2=A0 =C2=A0assert(b =3D=3D [1, 1, 1]); //Error: cannot implicitly conver= t expression (1) of type int to int[]<br> }<br> <br> <br> <br> I'd like D to require =C2=A0a[]=3D1 =C2=A0in that first case too.<br> <br> I'd like the [] to be required every time an O(n) vector operation is d= one, for:<br> - constancy with all other vector operations among two arrays, that require= [];<br> - and to avoid unwanted (and not easy to spot in the code) O(n) operations;= <br> - bugs and confusion in D newbies that don't have memorized all current= special cases.<br> <br> On the other hand Don says that [] is only required for lvalues.<br> <br> I think this boils to a new table like this:<br> <br> <br> Rhs is an array, is it compilable?<br> a =C2=A0 =C2=A0 =C2=A0 / b =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 a=3Db = =C2=A0 =C2=A0 a[]=3Db =C2=A0 a=3Db[] =C2=A0 a[]=3Db[]<br> int[3u] / int[3u] =C2=A0 =C2=A0 =C2=A0 FALSE =C2=A0 true =C2=A0 =C2=A0FALSE= =C2=A0 true<br> int[3u] / int[] =C2=A0 =C2=A0 =C2=A0 =C2=A0 FALSE =C2=A0 true =C2=A0 =C2=A0= FALSE =C2=A0 true<br> int[] =C2=A0 / int[3u] =C2=A0 =C2=A0 =C2=A0 FALSE =C2=A0 true =C2=A0 =C2=A0= FALSE =C2=A0 true<br> int[] =C2=A0 / int[] =C2=A0 =C2=A0 =C2=A0 =C2=A0 true =C2=A0 =C2=A0true =C2= =A0 =C2=A0true =C2=A0 =C2=A0true<br> <br> Rhs is a element, is it compilable?<br> a =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 a=3DN =C2=A0 =C2=A0 a[]=3DN =C2=A0 a[0..2]=3DN<br> int[3u] =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 FALSE =C2= =A0 true =C2=A0 =C2=A0true<br> int[] =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 false = =C2=A0 true =C2=A0 =C2=A0true<br> <br> <br> Now if there's a [] on the left, then it's an O(n) vector operation= (like a copy), otherwise it's O(1).<br> <br> That also means:<br> <br> void main() {<br> =C2=A0 =C2=A0int[] a =3D new int[3];<br> =C2=A0 =C2=A0int[] b =3D new int[3];<br> =C2=A0 =C2=A0a =3D b; // OK, copies just array fat reference<br> }<br> <br> void main() {<br> =C2=A0 =C2=A0int[3] a, b;<br> =C2=A0 =C2=A0a =3D b; // Not OK, hidden vector op<br> }<br> <br> <br> I am not sure this new table is fully correct, but it's a start. Fixes = of mistakes are welcomes.<br> <br> -----------------------<br> <br> This is an alternative proposal.<br> <br> On the other hand this vector op syntax doesn't currently compile:<br> <br> void main() {<br> =C2=A0 =C2=A0int[3] a, b;<br> =C2=A0 =C2=A0a[] +=3D b;<br> }<br> <br> <br> So if array assign is seen as a normal vector op, then the [] is needed on = the right too:<br> <br> <br> Rhs is an array, is it compilable?<br> a =C2=A0 =C2=A0 =C2=A0 / b =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 a=3Db = =C2=A0 =C2=A0 a[]=3Db =C2=A0 a=3Db[] =C2=A0 a[]=3Db[]<br> int[3u] / int[3u] =C2=A0 =C2=A0 =C2=A0 FALSE =C2=A0 FALSE =C2=A0 FALSE =C2= =A0 true<br> int[3u] / int[] =C2=A0 =C2=A0 =C2=A0 =C2=A0 FALSE =C2=A0 FALSE =C2=A0 FALSE= =C2=A0 true<br> int[] =C2=A0 / int[3u] =C2=A0 =C2=A0 =C2=A0 FALSE =C2=A0 FALSE =C2=A0 FALSE= =C2=A0 true<br> int[] =C2=A0 / int[] =C2=A0 =C2=A0 =C2=A0 =C2=A0 true =C2=A0 =C2=A0FALSE = =C2=A0 FALSE =C2=A0 true<br> <br> Rhs is a element, is it compilable?<br> a =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 a=3DN =C2=A0 =C2=A0 a[]=3DN =C2=A0 a[0..2]=3DN<br> int[3u] =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 FALSE =C2= =A0 true =C2=A0 =C2=A0true<br> int[] =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 false = =C2=A0 true =C2=A0 =C2=A0true<br> <br> <br> Where the two cases with dynamic arrays are syntax errors to keep more symm= etry:<br> <br> void main() {<br> =C2=A0 =C2=A0int[] a =3D new int[3];<br> =C2=A0 =C2=A0int[] b =3D new int[3];<br> =C2=A0 =C2=A0a[] =3D b; // error<br> =C2=A0 =C2=A0a =3D b[]; // error<br> }<br> <br> -----------------------<br> <br> Lot of time ago I have opened bug issue 3971 on this topic, but that Bugzil= la thread contains various mistakes and some parts of it are obsolete.<br> <br> Bye,<br> <span><font color=3D"#888888">bearophile<br> </font></span></blockquote></div><br> </div> --0016364d3101e3545504b1d5c9b2--

Nov 16 2011

a = b for two static arrays of the same type and size is ok for me, especially when the size is small. I get your point though and like a strict syntax, too. The notion of a vector operation always using [] seems natural.

Nov 16 2011

On Wed, 16 Nov 2011 04:24:25 +0100, bearophile <bearophileHUGS lycos.com> wrote: [Good stuff] Votes++

Nov 16 2011

On Wed, 16 Nov 2011 03:24:25 -0000, bearophile <bearophileHUGS lycos.com> wrote: +1 vote toward looking into this more. I like the idea of a more consistent syntax, and the [] requirement for vector operations vs single/simple assignment.Lot of time ago I have opened bug issue 3971 on this topic, but that Bugzilla thread contains various mistakes and some parts of it are obsolete.

So.. create a new BZ, put the content of your message here in it, and mark the old one a duplicate of the new one .. or mark the old one invalid, and simply create a new one with this content. -- Using Opera's revolutionary email client: http://www.opera.com/mail/

Nov 16 2011

On 11/16/2011 04:24 AM, bearophile wrote:The Bugzilla issues that I really care about is a not an useless long list, it's about fifteen items long, and this post is about one of them. I think current array slice assign syntax is a bit messy, and I think this should be addressed. Kenji Hara has recently written a very nice program (that here I have modified a bit for clarity) that shows the current array assign situation: import std.stdio, std.typetuple; void main() { writeln("Rhs is an array, is it compilable?"); writeln("a\t/ b\t\ta=b\ta[]=b\ta=b[]\ta[]=b[]"); foreach (i, Lhs; TypeTuple!(int[3], int[])) foreach (j, Rhs; TypeTuple!(int[3], int[])) { writef("%s\t/ %s ", Lhs.stringof, Rhs.stringof); Lhs a = [0,0,0]; Rhs b = [1,2,3]; writef("\t%s", __traits(compiles, { a = b; })); writef("\t%s", __traits(compiles, { a[] = b; })); writef("\t%s", __traits(compiles, { a = b[]; })); writef("\t%s", __traits(compiles, { a[] = b[]; })); writeln(); } writeln("\nRhs is a element, is it compilable?"); writeln("a\t\t\ta=N\ta[]=N\ta[0..2]=N"); foreach (Lhs; TypeTuple!(int[3], int[])) { writef("%s\t\t", Lhs.stringof); Lhs a = [0,0,0]; writef("\t%s", __traits(compiles, { a = 9; })); writef("\t%s", __traits(compiles, { a[] = 9; })); writef("\t%s", __traits(compiles, { a[0..2] = 9; })); writeln(); } } Currently (DMD 2.057head, despite I think Walter has not updated DMD version number yet) it prints: Rhs is an array, is it compilable? a / b a=b a[]=b a=b[] a[]=b[] int[3u] / int[3u] true true true true int[3u] / int[] true true true true int[] / int[3u] true true true true int[] / int[] true true true true Rhs is a element, is it compilable? a a=N a[]=N a[0..2]=N int[3u] true true true int[] false true true This also means this is currently accepted: void main() { int[3] a; a = 1; assert(a == [1, 1, 1]); } While this is not accepted: void main() { int[] b = new int[3]; b = 1; assert(b == [1, 1, 1]); //Error: cannot implicitly convert expression (1) of type int to int[] } I'd like D to require a[]=1 in that first case too. I'd like the [] to be required every time an O(n) vector operation is done, for: - constancy with all other vector operations among two arrays, that require []; - and to avoid unwanted (and not easy to spot in the code) O(n) operations; - bugs and confusion in D newbies that don't have memorized all current special cases. On the other hand Don says that [] is only required for lvalues. I think this boils to a new table like this: Rhs is an array, is it compilable? a / b a=b a[]=b a=b[] a[]=b[] int[3u] / int[3u] FALSE true FALSE true int[3u] / int[] FALSE true FALSE true int[] / int[3u] FALSE true FALSE true int[] / int[] true true true true Rhs is a element, is it compilable? a a=N a[]=N a[0..2]=N int[3u] FALSE true true int[] false true true Now if there's a [] on the left, then it's an O(n) vector operation (like a copy), otherwise it's O(1). That also means: void main() { int[] a = new int[3]; int[] b = new int[3]; a = b; // OK, copies just array fat reference } void main() { int[3] a, b; a = b; // Not OK, hidden vector op } I am not sure this new table is fully correct, but it's a start. Fixes of mistakes are welcomes. ----------------------- This is an alternative proposal. On the other hand this vector op syntax doesn't currently compile: void main() { int[3] a, b; a[] += b; } So if array assign is seen as a normal vector op, then the [] is needed on the right too: Rhs is an array, is it compilable? a / b a=b a[]=b a=b[] a[]=b[] int[3u] / int[3u] FALSE FALSE FALSE true int[3u] / int[] FALSE FALSE FALSE true int[] / int[3u] FALSE FALSE FALSE true int[] / int[] true FALSE FALSE true Rhs is a element, is it compilable? a a=N a[]=N a[0..2]=N int[3u] FALSE true true int[] false true true Where the two cases with dynamic arrays are syntax errors to keep more symmetry: void main() { int[] a = new int[3]; int[] b = new int[3]; a[] = b; // error a = b[]; // error } ----------------------- Lot of time ago I have opened bug issue 3971 on this topic, but that Bugzilla thread contains various mistakes and some parts of it are obsolete. Bye, bearophile

First thing: int[3] a=3; // kill it!! Rest: a[] is _just a shortcut_ for a[0..$]! Are you really suggesting to disallow slicing? You are thinking too much in terms of syntax and not enough in terms of semantics. They are basically two distinct things involved here: 1. static arrays and lvalue slices 2. dynamic arrays and rvalue slices 1 implies value semantics, 2 implies reference semantics, where value semantics overrides reference semantics. Any other distinction is more or less arbitrary. As you pointed out, the main indicator of distinction is value vs reference semantics of the performed assignments. We certainly agree on this: Rhs is an array, is it compilable? a / b a=b a[]=b a=b[] a[]=b[] int[3u] / int[3u] ? ? ? true int[3u] / int[] ? ? ? true int[] / int[3u] ? ? ? true int[] / int[] ? ? ? true Now, a dynamic array a is equivalent to a[], and a static array b is equivalent to an lvalue slice b[]=. This gives the following equivalence classes of operations: Rhs is an array, is it compilable? a / b a=b a[]=b a=b[] a[]=b[] int[3u] / int[3u] 1 1 2 2 int[3u] / int[] 2 2 2 2 int[] / int[3u] 3 1 4 2 int[] / int[] 4 2 4 2 Any of the same class should behave the same. Now, you suggest in both proposals to allow at least one of class 2 and at least one of class 4. Filling all those out delivers: Rhs is an array, is it compilable? a / b a=b a[]=b a=b[] a[]=b[] int[3u] / int[3u] (1) (1) true true int[3u] / int[] true true true true int[] / int[3u] (3) (1) true true int[] / int[] true true true true 1 is "assign value to value". 3. is "assign value to reference". The upper left corner should certainly be true, values of any mutable type should be able to be assigned to themselves. This leaves: Rhs is an array, is it compilable? a / b a=b a[]=b a=b[] a[]=b[] int[3u] / int[3u] true true true true int[3u] / int[] true true true true int[] / int[3u] (3) true true true int[] / int[] true true true true 3 is the odd thing out. Now let's think about it, what should: int[] a; int[3] b; a=b; do? The answer is, there are two options. 1. implicitly slice b 2. copy b by value into a One is as arbitrary as the other, so it should be disallowed in a sane design. Which leaves: Rhs is an array, is it compilable? a / b a=b a[]=b a=b[] a[]=b[] int[3u] / int[3u] true true true true int[3u] / int[] true true true true int[] / int[3u] FALSE true true true int[] / int[] true true true true Rhs is a element, is it compilable? a a=N a[]=N a[0..2]=N int[3u] FALSE true true int[] false true true And that is how it should be.

Nov 16 2011

On Wed, 16 Nov 2011 19:47:47 +0100, Timon Gehr <timon.gehr gmx.ch> wrote:On 11/16/2011 04:24 AM, bearophile wrote:

First thing: int[3] a=3; // kill it!! Rest: a[] is _just a shortcut_ for a[0..$]! Are you really suggesting to disallow slicing? You are thinking too much in terms of syntax and not enough in terms of semantics. They are basically two distinct things involved here: 1. static arrays and lvalue slices 2. dynamic arrays and rvalue slices 1 implies value semantics, 2 implies reference semantics, where value semantics overrides reference semantics. Any other distinction is more or less arbitrary. As you pointed out, the main indicator of distinction is value vs reference semantics of the performed assignments. We certainly agree on this: Rhs is an array, is it compilable? a / b a=b a[]=b a=b[] a[]=b[] int[3u] / int[3u] ? ? ? true int[3u] / int[] ? ? ? true int[] / int[3u] ? ? ? true int[] / int[] ? ? ? true Now, a dynamic array a is equivalent to a[], and a static array b is equivalent to an lvalue slice b[]=. This gives the following equivalence classes of operations: Rhs is an array, is it compilable? a / b a=b a[]=b a=b[] a[]=b[] int[3u] / int[3u] 1 1 2 2 int[3u] / int[] 2 2 2 2 int[] / int[3u] 3 1 4 2 int[] / int[] 4 2 4 2 Any of the same class should behave the same. Now, you suggest in both proposals to allow at least one of class 2 and at least one of class 4. Filling all those out delivers: Rhs is an array, is it compilable? a / b a=b a[]=b a=b[] a[]=b[] int[3u] / int[3u] (1) (1) true true int[3u] / int[] true true true true int[] / int[3u] (3) (1) true true int[] / int[] true true true true 1 is "assign value to value". 3. is "assign value to reference". The upper left corner should certainly be true, values of any mutable type should be able to be assigned to themselves. This leaves: Rhs is an array, is it compilable? a / b a=b a[]=b a=b[] a[]=b[] int[3u] / int[3u] true true true true int[3u] / int[] true true true true int[] / int[3u] (3) true true true int[] / int[] true true true true 3 is the odd thing out. Now let's think about it, what should: int[] a; int[3] b; a=b; do? The answer is, there are two options. 1. implicitly slice b 2. copy b by value into a

In that case a should be be a slice of the static array b. It could make sense to require b[] for the conversion, but copying is no option.One is as arbitrary as the other, so it should be disallowed in a sane design. Which leaves: Rhs is an array, is it compilable? a / b a=b a[]=b a=b[] a[]=b[] int[3u] / int[3u] true true true true int[3u] / int[] true true true true int[] / int[3u] FALSE true true true int[] / int[] true true true true Rhs is a element, is it compilable? a a=N a[]=N a[0..2]=N int[3u] FALSE true true int[] false true true And that is how it should be.

Nov 17 2011

On 11/17/2011 09:12 AM, Martin Nowak wrote:On Wed, 16 Nov 2011 19:47:47 +0100, Timon Gehr <timon.gehr gmx.ch> wrote:On 11/16/2011 04:24 AM, bearophile wrote:

First thing: int[3] a=3; // kill it!! Rest: a[] is _just a shortcut_ for a[0..$]! Are you really suggesting to disallow slicing? You are thinking too much in terms of syntax and not enough in terms of semantics. They are basically two distinct things involved here: 1. static arrays and lvalue slices 2. dynamic arrays and rvalue slices 1 implies value semantics, 2 implies reference semantics, where value semantics overrides reference semantics. Any other distinction is more or less arbitrary. As you pointed out, the main indicator of distinction is value vs reference semantics of the performed assignments. We certainly agree on this: Rhs is an array, is it compilable? a / b a=b a[]=b a=b[] a[]=b[] int[3u] / int[3u] ? ? ? true int[3u] / int[] ? ? ? true int[] / int[3u] ? ? ? true int[] / int[] ? ? ? true Now, a dynamic array a is equivalent to a[], and a static array b is equivalent to an lvalue slice b[]=. This gives the following equivalence classes of operations: Rhs is an array, is it compilable? a / b a=b a[]=b a=b[] a[]=b[] int[3u] / int[3u] 1 1 2 2 int[3u] / int[] 2 2 2 2 int[] / int[3u] 3 1 4 2 int[] / int[] 4 2 4 2 Any of the same class should behave the same. Now, you suggest in both proposals to allow at least one of class 2 and at least one of class 4. Filling all those out delivers: Rhs is an array, is it compilable? a / b a=b a[]=b a=b[] a[]=b[] int[3u] / int[3u] (1) (1) true true int[3u] / int[] true true true true int[] / int[3u] (3) (1) true true int[] / int[] true true true true 1 is "assign value to value". 3. is "assign value to reference". The upper left corner should certainly be true, values of any mutable type should be able to be assigned to themselves. This leaves: Rhs is an array, is it compilable? a / b a=b a[]=b a=b[] a[]=b[] int[3u] / int[3u] true true true true int[3u] / int[] true true true true int[] / int[3u] (3) true true true int[] / int[] true true true true 3 is the odd thing out. Now let's think about it, what should: int[] a; int[3] b; a=b; do? The answer is, there are two options. 1. implicitly slice b 2. copy b by value into a

In that case a should be be a slice of the static array b. It could make sense to require b[] for the conversion, but copying is no option.

My goal was to deduce consistent rules. Those are the only two options /for assignment behaviour of arrays/, and both are equally bad in this case: // this is your argument against value semantics: a = ...; // everywhere else this means reference assign // this is an argument against reference semantics: ... = b; // everywhere else this means copy by value // which leaves us with: a = b; // ???! So indeed, for the behaviour of a=b; none of them 'is an option'. I should probably have formulated that better. Thanks!One is as arbitrary as the other, so it should be disallowed in a sane design. Which leaves: Rhs is an array, is it compilable? a / b a=b a[]=b a=b[] a[]=b[] int[3u] / int[3u] true true true true int[3u] / int[] true true true true int[] / int[3u] FALSE true true true int[] / int[] true true true true Rhs is a element, is it compilable? a a=N a[]=N a[0..2]=N int[3u] FALSE true true int[] false true true And that is how it should be.

Nov 17 2011