Conditional purity

Simen kjaeraas simen.kjaras at gmail.com
Sun Jul 25 06:14:20 PDT 2010


bearophile <bearophileHUGS at lycos.com> wrote:

> But a bigger problem is that I can't see a way to define a conditionally  
> pure templated function, for example a map() that is pure if the given  
> function pointer is pure, and is not pure otherwise:

This is fairly simple currently:

template map( alias fn ) { // blah blah blah, whatever is necessary here
     static if ( isPure!fn ) {
         pure map( Range )( Range r ) { ... }
     } else {
         auto map( Range )( Range r ) { ... }
     }
}

Of course, this leads to code duplication, which is unwanted. In cases
like this, I long for some features of the C preprocessor, which would
be able to insert any kind of code wherever it wants.

This feature request, and Steve's problems with final (in
std.pattern..mixin temptes..std.concurrency), makes me think there
should be some way to specify all (or at least most) such conditionally.
There seems to be a real need for it.

-- 
Simen


More information about the Digitalmars-d mailing list