March 05, 2010 opEquals among types | ||||
---|---|---|---|---|
| ||||
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 |
March 06, 2010 Re: opEquals among types | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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.
|
Copyright © 1999-2021 by the D Language Foundation