array comparison

bearophile bearophileHUGS at lycos.com
Tue Feb 16 10:51:59 PST 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



More information about the Digitalmars-d mailing list