opDispatch and template parameters

Simen kjaeraas simen.kjaras at gmail.com
Tue Sep 14 18:36:45 PDT 2010


Guillaume B. <guillaume.b.spam at spam.ca> wrote:

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

>  Wow! That's a nice trick! Seems like I'll have to be carefull when  
> defining
> opDispatch!

It's the way the language works. Just like D does not support
automatic currying of functions, like this:

void foo( int n, string s ) {}
auto bar = foo( 4 );
assert( is( typeof( bar ) == void function ( string ) ) );

Neither does it support automatic currying for templates. Hence,
all arguments must be passed in one go. The above is in a way
doable in D, but only by jumping through hoops:

auto foo( int n ) {
     return ( string s ) {};
}
foo( 4 )( "my string!" );

The same is to an extent doable with templates, limited by
bug #242 [1]. opDispatch provides a respite from #242, by being
magical.

This way of programming (unary functions that return unary
functions [that...]) reminds one of the Lambda calculus[2].

[1]: http://d.puremagic.com/issues/show_bug.cgi?id=242
[2]: http://en.wikipedia.org/wiki/Lambda_calculus

-- 
Simen


More information about the Digitalmars-d mailing list