www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 1789] New: Template overloading doesn't work

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

           Summary: Template overloading doesn't work
           Product: D
           Version: 1.025
          Platform: PC
        OS/Version: All
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: aarti interia.pl


void pattern(T)(T v) {}

T pattern(T)() {return T.init;}

void main() {
    pattern!(int)(5);
}

---------

Above code results in compile time error:

quicktest.d(6): template instance pattern!(int) matches more than one template
declaration, pattern(T) and pattern(T)
quicktest.d(6): Error: template instance 'pattern!(int)' is not a variable
quicktest.d(6): Error: function expected before (), not pattern!(int) of type
int

---------

As you see it is standard D setter/getter approach with templates.

What's more interesting, when we call pattern template function with IFTI it
calls proper function:

pattern(5); //calls setter

but:
pattern; // is error

BTW: I hope that in DMD 2.0
writefln;
will be also allowed. Currently it doesn't work - you have to write writefln();

Ref:
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=10766




-- 
Jan 17 2008
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1789


bugzilla digitalmars.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |WONTFIX





That's the way it is designed to work, overloading of templates with the
template arguments overloads based on only the template argument list. You can
achieve what you wish with the following:

template pattern(T)
{
    void foo(T v) {}
    T foo() { return T.init; }
}

alias pattern!(int).foo bar;

void main() {
    pattern!(int).foo(5);

    pattern!(int).foo = 5;
    int x = pattern!(int).foo;

    bar = 5;
    int y = bar;
}


-- 
Aug 28 2008