www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Dictionary of Templated Functions

reply jwatson-CO-edu <real.name colorado.edu> writes:
Hello,
I'm trying to create a dictionary of templated function pointers. 
  The functions should return `bool` and take a differently-typed 
dynamics arrays `T[]` as an argument.  Based on my understanding 
of the docs, I've made two failed attempts:


**Attempt 1**:
https://github.com/PhilippeSigaud/D-templates-tutorial/blob/master/D-templates-tutorial.md
```
template ArrArgs(T){
     alias bool function(T[])[string] FuncDict; // compiles
     FuncDict lookup; // This compiles but name `lookup` is not 
understood
}

// Compiler does not allow me to declare `FuncDict lookup;` here

void main(){
     lookup["foo"] = function bool( string[] args ){ return true; 
}; // undefined identifier `lookup`
}
```


**Attempt 2**:
https://dlang.org/library/std/meta/alias.html
```
alias FuncDict = bool function(T[])[string]; // undefined 
identifier `T`

FuncDict lookup;

void main(){
     lookup["foo"] = function bool( string[] args ){ return true; 
};
}
```

I'm unsure of what feature or structure of D will accomplish this.
An example or a link to an example of associated arrays of 
templated function pointers would help me a great deal.
Sep 09 2022
parent reply frame <frame86 live.com> writes:
On Saturday, 10 September 2022 at 00:24:11 UTC, jwatson-CO-edu 
wrote:
 Hello,
 I'm trying to create a dictionary of templated function 
 pointers.  The functions should return `bool` and take a 
 differently-typed dynamics arrays `T[]` as an argument.
This won't work as you might expect. Your container would need to support multiple types and may need overloads of operators to become that magic. But lets begin with a simple type. Consider your example fixed: ```d alias FuncDict(T) = bool function(T[])[string]; // alias needs T as parameter too... FuncDict!(string) lookup; // ...so we can instantiate a FuncDict with T = string void main() { lookup["foo"] = function bool(string[] args) { return true; }; } ``` As you can see `T` is bound to `string` here and cannot be something else.
Sep 10 2022
parent reply jwatson-CO-edu <real.name colorado.edu> writes:
On Saturday, 10 September 2022 at 22:13:01 UTC, frame wrote:
 On Saturday, 10 September 2022 at 00:24:11 UTC, jwatson-CO-edu 
 wrote:
 Hello,
 I'm trying to create a dictionary of templated function 
 pointers.  The functions should return `bool` and take a 
 differently-typed dynamics arrays `T[]` as an argument.
This won't work as you might expect. Your container would need to support multiple types and may need overloads of operators to become that magic. But lets begin with a simple type. Consider your example fixed: ```d alias FuncDict(T) = bool function(T[])[string]; // alias needs T as parameter too... FuncDict!(string) lookup; // ...so we can instantiate a FuncDict with T = string void main() { lookup["foo"] = function bool(string[] args) { return true; }; } ``` As you can see `T` is bound to `string` here and cannot be something else.
Ah, this gives me more information on how to use `alias`es, thank you! I can see that I have wished for a magical thing. So, my solution will be to construct a catch-all struct `Payload` and have that be my argument type from which various functions can draw the data of their choice. ```d struct Payload{ double[] dbbls; string[] strns; // Probably others ... } ``` Application is a Dlang implementation of Little Scheme, from the book "The Little Schemer". For it, I need a menu of "primitive" base functions that form the foundation of anything you might ask the interpreter to do. (Hence my wish for a multi-type dict.) Thanks again!
Sep 10 2022
parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 9/10/22 15:35, jwatson-CO-edu wrote:

 So, my solution
 will be to construct a catch-all struct `Payload` and have that be my
 argument type from which various functions can draw the data of their
 choice.
Two Phobos features may be helpful there: https://dlang.org/phobos/std_sumtype.html https://dlang.org/phobos/std_variant.html Ali
Sep 10 2022