Passing around a list of differently typed functions

Alix Pexton via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jun 23 01:23:34 PDT 2014


On 23/06/2014 8:19 AM, Bienlein wrote:
> On Monday, 23 June 2014 at 01:16:49 UTC, Evan Davis wrote:
>> As the subject says, I would like to pass around an array of
>> functions. The trick is, that the functions have different type
>> signatures. Is there a way to put the two functions
>>
>> int foo(int a, int b);
>> bool bar(bool a, bool b);
>>
>> into one array, that I can pass around and cast as necessary?
>>
>> Thanks, Evan
>
> Have functions in the array without parpameters that call the function
> with the applicable parameters, e.g. int foo(int a, int b) or bool
> bar(bool a, bool b). That doesn't address the problem of the different
> return types (int and bool). So change the return type to void and
> return the value as an inout parameter.

You could convert your functions to return via an out parameter (either 
manually or with template magic) then have an array of same-signatured 
closures that can call any function.

A...


More information about the Digitalmars-d-learn mailing list