opEquals among types

bearophile bearophileHUGS at lycos.com
Fri Mar 5 12:04:56 PST 2010


This D2 program compiles and works correctly:

import std.c.stdio: printf;
auto add(T1, T2)(T1 x, T2 y) {
    if (!is(T1 == T2))
        printf("Different types\n");
    return x + y;
}
void main() {}


But do you know why D2 needs that is() there? Can't it be removed, like this? (doesn't work):

import std.c.stdio: printf;
auto add(T1, T2)(T1 x, T2 y) {
    if (T1 != T2)
        printf("Different types\n");
    return x + y;
}
void main() {}


The difference for the programmer is not big, but the second is a little shorter/cleaner.

Bye and thank you,
bearophile


More information about the Digitalmars-d-learn mailing list