www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Passing around a list of differently typed functions

reply "Evan Davis" <cptroot gmail.com> writes:
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
Jun 22 2014
next sibling parent "Tobias Pankrath" <tobias pankrath.net> writes:
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
Use void* as a common type or use std.variant.
Jun 22 2014
prev sibling next sibling parent reply "FreeSlave" <freeslave93 gmail.com> writes:
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
You can pass them as pointers, for example cast to void*. But you still need correct signature to cast pointer to actual type before call function.
Jun 22 2014
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 06/22/2014 11:32 PM, FreeSlave 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
You can pass them as pointers, for example cast to void*. But you still need correct signature to cast pointer to actual type before call
function. In C and C++, void* is for data pointers only. As function pointers are a different kind of beast, casting to and from void* is undefined behavior. (Note: It works on all common platforms.) I wonder whether D has any decision on that. Ali
Jun 23 2014
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 23 Jun 2014 14:30:12 -0400, Ali =C3=87ehreli <acehreli yahoo.com=
 wrote:
 On 06/22/2014 11:32 PM, FreeSlave 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
  >
  > You can pass them as pointers, for example cast to void*. But you  =
 still
  > need correct signature to cast pointer to actual type before call  =
 function.

 In C and C++, void* is for data pointers only. As function pointers ar=
e =
 a different kind of beast, casting to and from void* is undefined  =
 behavior. (Note: It works on all common platforms.)
Wow, really? That is strange.
 I wonder whether D has any decision on that.
I would hope it's defined. A pointer is a pointer. -Steve
Jun 23 2014
prev sibling parent reply "Bienlein" <jeti789 web.de> writes:
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.
Jun 23 2014
parent Alix Pexton <alix.DOT.pexton gmail.DOT.com> writes:
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...
Jun 23 2014