Using opApply and Attributes

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Nov 20 08:50:48 PST 2016


On Sunday, 20 November 2016 at 16:36:18 UTC, Q. Schroll wrote:
> How can I have relative- at attrib functions without unnecessary 
> manual overloading?


import std.traits;

auto f1(DG) (DG dg) if (isCallable!DG && Parameters!DG.length == 
1 && is(Parameters!DG[0] == int)) { // check other things here 
too, like return type
   return dg(1);
}

void main () pure {
   f1((int x) => 2*x); // ok, f1 is pure
   //f1(x => 2*x); // alas, now you have to manually specify 
argument types
}


not the best solution, of course, and you can't do that without 
templates ('cause `nothrow` or `@nogc` *can* require different 
machine code in the future).


More information about the Digitalmars-d-learn mailing list