View mode: basic / threaded / horizontal-split · Log in · Help
January 06, 2005
False matches in AA of structs
Using DMD 0.110, Windows 98SE.

Just thinking about my last post, it's ironic that AAs require opEquals 
to be defined in a certain way, as the mechanism seems to be just 
ignoring opEquals altogether!  AAs support classes where unequal objects 
can rank equally in order.  Surely they should work on structs with the 
same characteristic?

----------
import std.stdio;
import std.ctype;

struct IChar {
    char data;

    int opEquals(IChar* i) {
        return data == i.data;
    }

    int opEquals(IChar i) {
        return opEquals(&i);
    }

    uint toHash() {
        return toupper(data);
    }

    int opCmp(IChar* i) {
        return toupper(data) - toupper(i.data);
    }

    int opCmp(IChar i) {
        return opCmp(&i);
    }
}


int main() {
    int[IChar] aa;

    static IChar c1 = { 'c' };
    static IChar c2 = { 'c' };
    static IChar c3 = { 'C' };

    assert (c1 == c2);
    assert (c2 != c3);

    aa[c1] = 4;

    if (c1 in aa) {
        writefln(aa[c1]);
    } else {
        writefln("No such element");
    }

    if (c2 in aa) {
        writefln(aa[c2]);
    } else {
        writefln("No such element");
    }

    if (c3 in aa) {
        writefln(aa[c3]);
    } else {
        writefln("No such element");
    }

    return 0;
}
----------

Actual output:
4
4
4

Expected output:
4
4
No such element

Stewart.

-- 
My e-mail is valid but not my primary mailbox.  Please keep replies on 
the 'group where everyone may benefit.
Top | Discussion index | About this forum | D home