www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4576] New: accepts-invalid: 0/1 argument calls to overloaded function is allowed in presence of variadic function

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4576

           Summary: accepts-invalid: 0/1 argument calls to overloaded
                    function is allowed in presence of variadic function
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: andrej.mitrovich gmail.com


--- Comment #0 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2010-08-03
12:27:15 PDT ---
According to TDPL, this should be an ambiguous call and issue a compiler error:

import std.algorithm, std.array, std.stdio;          

 property bool empty(T)(T[] a) { return a.length == 0; }
 property ref T front(T)(T[] a) { return a[0]; }
void popFront(T)(ref T[] a) { a = a[1 .. $]; }

V reduce(alias fun, V, R)(V x, R range)
{
    for ( ; !range.empty; range.popFront()) {
        x = fun(x, range.front);
    }
    return x;
}

double average() { return 2.0; }
double average(double) { return 3.0; }

// compute the avg of a set of numbers, passable directly or via an array
double average(double[] values...)
{
    if (values.empty)
    {
        throw new Exception("Average of zero elements is undefined");
    }
    return reduce!((a, b) { return a + b; })(0.0, values) / values.length;
}

unittest {
    average();  // This should not compile, but does

    writeln(average());  // calls average(), returns 2.0
    writeln(average(0)); // calls average(double), returns 3.0

    assert(average(0) == 0);  // fails, since it calls average() which returns
2.0
    assert(average(1, 2) == 1.5);
    assert(average(1, 2, 3) == 2);

    // passing arrays and slices works too
    double[] v = [1, 2, 3];
    assert(average(v) == 2);
}

void main() { }

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 03 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4576



--- Comment #1 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2010-08-03
12:29:27 PDT ---
Sorry for the mixed comments between TDPL and the ones I've added. The
average(); call should issue a compiler error. And a 1-argument call average(0)
should issue an error as well.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 03 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4576


nfxjfg gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |accepts-invalid
                 CC|                            |nfxjfg gmail.com
            Summary|accepts-invalid: 0/1        |0/1 argument calls to
                   |argument calls to           |overloaded function is
                   |overloaded function is      |allowed in presence of
                   |allowed in presence of      |variadic function
                   |variadic function           |


--- Comment #2 from nfxjfg gmail.com 2010-08-03 12:42:42 PDT ---
Keywords go into keyword field.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 03 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4576



--- Comment #3 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2010-08-03
12:54:15 PDT ---
(In reply to comment #2)
 Keywords go into keyword field.
Ah, thanks! -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Aug 03 2010
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4576


Andrej Mitrovic <andrej.mitrovich gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         OS/Version|Windows                     |All


--- Comment #4 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2013-09-17
12:48:04 PDT ---
Reduced test-case:

-----
void test(int[] arr...) { assert(1); }
void test() { assert(0); }  // hijacks (shadows) above function

void main()
{
    test();  // should fail at CT, not RT.
}
-----

Comment out the second function and the first one will be called. The `test()`
call should be ambiguous and issue a compile-time error.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 17 2013