www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - opEquals among types

reply bearophile <bearophileHUGS lycos.com> writes:
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
Mar 05 2010
parent Jacob Carlborg <doob me.com> writes:
On 3/5/10 21:04, bearophile wrote:
 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
I think that this has been up for discussion before and if I recall correctly the answer was that it would make the compiler more complicated.
Mar 06 2010