Shouldn't duplicate functions be caught by DMD?

Andrej Mitrovic none at none.none
Sat May 7 17:56:16 PDT 2011


I'm not talking about function overloading, but functions with the same parameters inside the same class definition:

class Foo
{
    int foo(int i)
    {
        return 1;
    }
    
    int foo(int i)
    {
        return 1;
    }    
    
    void bar()
    {
        foo(1);
    }
}

void main()
{
    auto foo = new Foo();
}

Errors:
test.d(19): Error: function test.Foo.foo called with argument types:
        ((int))
matches both:
        test.Foo.foo(int i)
and:
        test.Foo.foo(int i)

If you comment out the call to foo(), and compile via -c -w -wi, no errors will be emitted. Only later when you try to use the object file you'll get a linker error:

fset 003C4H Record Type 00C3
 Error 1: Previous Definition Different : _D4test3Foo3fooMFiZi
--- errorlevel 1

I think the compiler should check catch these mistakes at compile-time.


More information about the Digitalmars-d-learn mailing list