1 matches bool, 2 matches long

kenji hara k.hara.pg at gmail.com
Sat Apr 27 04:41:16 PDT 2013


First, I can guess that why Walter disagree *fixing* this problem.

http://dlang.org/overview.html
> Major Design Goals of D
> 9. Where D code looks the same as C code, have it either behave the same
or issue an error.

Based on the design goal, we should not change the behavior toward foo(1)
matching to long version.
It will change the code behavior that looks like C.

But, we can raise an "ambiguous error" for the case.
I think we need to add a "special rule" for more natural overload
resolution in D.

// The expected behavior we can do at the most
extern(C) int printf(const char*, ...);
void foo(bool) { printf("bool\n"); }
void foo(long) { printf("long\n"); }
void main()
{
  foo(0);  // Error: function foo called with argument types: (int) matches
both foo(bool) and foo(long)
  foo(1);  // Error: function foo called with argument types: (int) matches
both foo(bool) and foo(long)
  foo(2);  // OK, matches to long
}

Kenji Hara

2013/4/27 Walter Bright <newshound2 at digitalmars.com>

> On 4/26/2013 1:59 PM, Andrej Mitrovic wrote:
>
>> On 4/26/13, Andrej Mitrovic <andrej.mitrovich at gmail.com> wrote:
>>
>>> An even better example:
>>>
>>
>> import std.stdio;
>>
>> void foo(bool x) { writeln("1"); }
>> void foo(long x) { writeln("2"); }
>>
>> void main()
>> {
>>      foo(1);  // "1"
>>      foo(false ? 2 : 1);  // "2"
>> }
>>
>> Kill it with fire.
>>
>
> How about this one:
>
> import std.stdio;
>
> void foo(short x) { writeln("1"); }
>
> void foo(long x) { writeln("2"); }
>
> void main()
> {
>      foo(30000);  // "1"
>      foo(false ? 40000 : 30000);  // "2"
> }
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20130427/fa743178/attachment.html>


More information about the Digitalmars-d mailing list