D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4856 - opDispatch does not work with specified template parameters
Summary: opDispatch does not work with specified template parameters
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Linux
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-09-12 18:09 UTC by Guillaume Benny
Modified: 2010-09-14 17:44 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Guillaume Benny 2010-09-12 18:09:06 UTC
opDispatch does accept additional template parameters but they cannot be specified:

====
module test.d;

import std.stdio;

struct DispatchTest {
    void opDispatch(string name, string otherName)() {
        writeln(name, ":", otherName);
    }
}

void main() {
    DispatchTest t;
    //t.testName!("testOtherName")();
    t.opDispatch!("testName", "testOtherName")();
}
====

 This compiles fine but if I remove the commented line, dmd (v2.048) tells 
me:

test.d(13): Error: template instance opDispatch!("testName") does not match 
template declaration opDispatch(string name,string otherName)

The error seems OK for a "normal" function, but for opDispatch, it seems 
limiting to me: the first call should translate to the second.

Here's an other, similar, test:

====
module test.d;

import std.stdio;

struct DispatchTest {
    void opDispatch(string name, T)(T t) {
        writeln(name, ":", T.stringof);
    }
}

void main() {
    DispatchTest t;
    //t.testName!(DispatchTest)(t);
    t.testName(t);
}
====

Which gives, when uncommenting:

test.d(13): Error: template instance opDispatch!("testName") does not match 
template declaration opDispatch(string name,T).
Comment 1 Guillaume Benny 2010-09-14 17:44:47 UTC
Simen Kjaeraas found a solution:

http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=117353

===
The correct way to do what you want is this: 

module foo; 

import std.stdio; 

struct test { 
     template opDispatch( string name ) { 
         void opDispatch( string other )( ) { 
             writeln( name, ", ", other ); 
         } 
     } 
} 

void main( ) { 
     test t; 
     t.foo!( "Hey!" )( ); 
} 
===