digitalmars.D - array comparison
- Andrei Alexandrescu (8/8) Feb 15 2010 I think comparing arrays should work if comparison of individual
- Jonathan M Davis (6/17) Feb 15 2010 It strikes me as a very useful feature and would go well with D's abilit...
- Michel Fortin (25/34) Feb 16 2010 I think it makes sense. An argument could be made that it's slower so
- bearophile (3/3) Feb 16 2010 I want equals among arrays and dynamic arrays and structs/Tuples that co...
- BCS (7/18) Feb 16 2010 I'm for it but with a different syntax because it's an element by elemen...
- bearophile (5/9) Feb 16 2010 Gah, no thanks -.-
- BCS (5/16) Feb 16 2010 I'm not following what you're saying there.
- bearophile (25/26) Feb 16 2010 Sorry, English isn't my first language, so sometimes what I write is wro...
- BCS (8/19) Feb 16 2010 [...]
- Simen kjaeraas (5/13) Feb 16 2010 Absolutely.
I think comparing arrays should work if comparison of individual elements should work. int[] a; long[] b; assert(a == b); A minimalistic-motivated counter-argument could be put forth, but I think we want smart minimalistic, not dumb minimalistic. Andrei
Feb 15 2010
Andrei Alexandrescu wrote:I think comparing arrays should work if comparison of individual elements should work. int[] a; long[] b; assert(a == b); A minimalistic-motivated counter-argument could be put forth, but I think we want smart minimalistic, not dumb minimalistic. AndreiIt strikes me as a very useful feature and would go well with D's ability to copy whole arrays at once rather than having to copy all of the elements individually. On the surface, at least, I see no reason why it would cause any problems to allow it. It would certainly make the language nicer. - Jonathan M Davis
Feb 15 2010
On 2010-02-16 00:53:31 -0500, Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> said:I think comparing arrays should work if comparison of individual elements should work. int[] a; long[] b; assert(a == b); A minimalistic-motivated counter-argument could be put forth, but I think we want smart minimalistic, not dumb minimalistic.I think it makes sense. An argument could be made that it's slower so you should have a separate function to handle it however, like "walkEquals". But I don't think that's necessary. On another note, I think comparing structs should work when you have an array in it. I wrote a struct like this recently: struct C { int i; string s; } And found out that the compiler compares them with a bitwise comparison instead of an element-wise one, and since two strings might be equal but not have the same underling pointer it makes the structs unequal. I've also found that comparing associative arrays doesn't work by comparing the content, as illustrated by this: int[string] aa = ["a":1,"b":2]; assert(aa == ["a":1,"b":2]); // fails To return to the original subject, should a comparison between int[string] and long[wstring] work? I think equality needs to be revised as a whole. -- Michel Fortin michel.fortin michelf.com http://michelf.com/
Feb 16 2010
I want equals among arrays and dynamic arrays and structs/Tuples that contain arrays. Bye, bearophile
Feb 16 2010
Hello Andrei,I think comparing arrays should work if comparison of individual elements should work. int[] a; long[] b; assert(a == b); A minimalistic-motivated counter-argument could be put forth, but I think we want smart minimalistic, not dumb minimalistic.I'm for it but with a different syntax because it's an element by element operation: assert(a[] == b[]); (that is unless I'm misremembering how the other array ops work)Andrei-- <IXOYE><
Feb 16 2010
BCS:I'm for it but with a different syntax because it's an element by element operation: assert(a[] == b[]); (that is unless I'm misremembering how the other array ops work)Gah, no thanks -.- (What's the purpose of == among arrays then?) Bye, bearophile
Feb 16 2010
Hello bearophile,BCS:If you don't like the syntax I can understand that but...I'm for it but with a different syntax because it's an element by element operation: assert(a[] == b[]); (that is unless I'm misremembering how the other array ops work)Gah, no thanks -.-(What's the purpose of == among arrays then?)I'm not following what you're saying there.Bye, bearophile-- ... <IXOYE><
Feb 16 2010
BCS:I'm not following what you're saying there.Sorry, English isn't my first language, so sometimes what I write is wrong. Let me explain better. In a program like: void main() { auto a = new int[4]; auto b = new int[4]; auto c = new int[4]; c = a + b; // compile-time error } DMD raises a compilation error (the message it prints is wrong, but this is beside the point). You have to use: c[] = a[] + b[]; If you want to denote the equality among AAs and Dyn arrays with []==[] like this: void main() { auto a = new int[4]; auto b = new int[4]; bool eq1 = a[] == b[]; int [int] aa1, aa2; bool eq2 = aa1[] == aa2[]; } Then a==b and aa1==aa2 have to become syntax errors, otherwise it's too much easy for the programmer to forget the [] and hope the compiler will compare the items of the arrays. Note: LDC already implements == among AAs, see "DMD incompatibilities" here: http://www.dsource.org/projects/ldc/wiki/Docs http://d.puremagic.com/issues/show_bug.cgi?id=1429 Bye, bearophile
Feb 16 2010
Hello bearophile,BCS:English *is* my first language, and sometimes what I write is wrong. :)I'm not following what you're saying there.Sorry, English isn't my first language, so sometimes what I write is wrong.Let me explain better. In a program like:[...]Then a==b and aa1==aa2 have to become syntax errors, otherwise it's too much easy for the programmer to forget the [] and hope the compiler will compare the items of the arrays.I'm fine with that solution. If you want to compare identity use: (a.ptr is b.ptr && a.length == b.length) or maybe "a is b" could be used. -- ... <IXOYE><
Feb 16 2010
On Tue, 16 Feb 2010 06:53:31 +0100, Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> wrote:I think comparing arrays should work if comparison of individual elements should work. int[] a; long[] b; assert(a == b); A minimalistic-motivated counter-argument could be put forth, but I think we want smart minimalistic, not dumb minimalistic. AndreiAbsolutely. -- Simen
Feb 16 2010