Two functions with different args. Taking address of the one

Paul Backus snarwin at gmail.com
Thu Mar 11 13:14:56 UTC 2021


On Thursday, 11 March 2021 at 12:56:34 UTC, Виталий Фадеев wrote:
>
> This will generate lambda:
>   __processMouseKey = (ref MouseKeyEvent event) { 
> process(event); };
>
> two calls:
>   call labnda;
>     call process;
>
> What right way to call function directly with selecting one of 
> two ?

Something like this:

     template Overloads(alias symbol)
     {
         static if (__traits(compiles, __traits(parent, symbol)))
             alias Overloads = __traits(getOverloads,
                 __traits(parent, symbol),
                 __traits(identifier, symbol)
             );
         else
             alias Overloads = symbol;
     }

     auto getOverloadFor(alias fun, T)()
     {
         foreach (overload; Overloads!fun)
             static if (__traits(compiles, (T arg) { 
overload(arg); }))
                 return &overload;
     }

Usage:

     __processMouseKey = getOverloadFor!(process, MouseKeyEvent);


More information about the Digitalmars-d-learn mailing list