Thread overview
Dictionary of Templated Functions
Sep 10, 2022
jwatson-CO-edu
Sep 10, 2022
frame
Sep 10, 2022
jwatson-CO-edu
Sep 10, 2022
Ali Çehreli
September 10, 2022

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.

September 10, 2022

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:

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.

September 10, 2022

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:

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.
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!
September 10, 2022
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