www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - array comparison

reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
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
next sibling parent Jonathan M Davis <jmdavisProg gmail.com> writes:
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.
 
 Andrei

It 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
prev sibling next sibling parent reply Michel Fortin <michel.fortin michelf.com> writes:
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
parent bearophile <bearophileHUGS lycos.com> writes:
I want equals among arrays and dynamic arrays and structs/Tuples that contain
arrays.

Bye,
bearophile
Feb 16 2010
prev sibling next sibling parent reply BCS <none anon.com> writes:
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
parent reply bearophile <bearophileHUGS lycos.com> writes:
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
parent reply BCS <none anon.com> writes:
Hello bearophile,

 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)


If you don't like the syntax I can understand that but...
 (What's the purpose of == among arrays then?)

I'm not following what you're saying there.
 Bye,
 bearophile

... <IXOYE><
Feb 16 2010
parent reply bearophile <bearophileHUGS lycos.com> writes:
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
parent BCS <none anon.com> writes:
Hello bearophile,

 BCS:
 
 I'm not following what you're saying there.
 

wrong.

English *is* my first language, and 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
prev sibling parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
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.

 Andrei

Absolutely. -- Simen
Feb 16 2010