array comparison

Michel Fortin michel.fortin at michelf.com
Tue Feb 16 01:10:21 PST 2010


On 2010-02-16 00:53:31 -0500, Andrei Alexandrescu 
<SeeWebsiteForEmail at 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 at michelf.com
http://michelf.com/




More information about the Digitalmars-d mailing list