Bug 1429 – Equality for associative arrays doesn't work
Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
PowerPC
OS
Mac OS X
Creation time
2007-08-18T04:13:00Z
Last change time
2014-02-15T13:12:57Z
Keywords
spec, wrong-code
Assigned to
nobody
Creator
josh
Comments
Comment #0 by josh — 2007-08-18T04:13:51Z
Sample code below along with a basic equals implementation to show they are equal in content.
import std.stdio;
bool equals(T, U)(T[U] container, T[U] other) {
T[U] tmp = container;
foreach(U key, T value; other) {
if(!(key in container && value == container[key])) {
return false;
}
}
return true;
}
void main() {
int[char[]] a = ["hello"[]:1, "test"[]:2];
int[char[]] b = ["hello"[]:1, "test"[]:2];
writefln( a == a ); //=> true
writefln( a == b ); //=> false
writefln( a.equals(b) ); //=> true
writefln( ["hello"[]:1, "test"[]:2] == ["hello"[]:1, "test"[]:2] ); //=> false
writefln( ["hello"[]:1, "test"[]:2].equals(["hello"[]:1, "test"[]:2]) ); //=> true
writefln( a is a ); //=> obviously true
writefln( a is b ); //=> obviously false just checking
}
looks like equality is linked with identity. Is this a bug?
Comment #1 by smjg — 2007-08-24T19:27:09Z
BTW there's a bug in your equals implementation: it ignores keys that are in container but not in other. The easiest way to fix ti is to add this at the beginning of the function:
if (container.length != other.length) return false;
What is tmp for? Your code doesn't use it at all.
Comment #2 by smjg — 2007-10-11T15:14:12Z
Since when has DMD for Mac OS X existed?
Comment #3 by fvbommel — 2007-10-11T15:42:06Z
(In reply to comment #2)
> Since when has DMD for Mac OS X existed?
I'd guess since about the time Wine started supporting Intel Macs :P
(Theoretically, I haven't heard of anyone running such a setup)
Comment #4 by fvbommel — 2007-10-11T15:44:37Z
(In reply to comment #3)
> (In reply to comment #2)
> > Since when has DMD for Mac OS X existed?
>
> I'd guess since about the time Wine started supporting Intel Macs :P
> (Theoretically, I haven't heard of anyone running such a setup)
Alternatively, this is a bug in the frontend that was reported from a Mac, and the submitter forgot to set the OS box to 'All'.
Comment #5 by kamm-removethis — 2009-06-21T10:16:23Z
Fixed in LDC by http://www.dsource.org/projects/ldc/changeset/1512 . The spec could be fixed by adding
"Two associative arrays are equal if they have the same keys and the values of each key compare equal."
Comment #6 by smjg — 2009-08-25T10:55:25Z
(In reply to comment #5)
> Fixed in LDC by http://www.dsource.org/projects/ldc/changeset/1512 . The spec
> could be fixed by adding
>
> "Two associative arrays are equal if they have the same keys and the values of
> each key compare equal."
Shouldn't it care about the value _associated with_ each key, rather than just the value _of_ each key?
Comment #7 by bearophile_hugs — 2010-02-19T02:24:03Z