Associative Arrays with elements having function type?

kdevel kdevel at vogtner.de
Mon Nov 3 21:29:47 UTC 2025


Yesterday I read

     3. Neither the KeyTypes nor the element types of an 
associative array
     can be function types or void.

in <https://dlang.org/spec/hash-map.html>. Does the program below 
work only accidentally?

```d
import std;

void foo () { writeln (__PRETTY_FUNCTION__); }

void bar () { writeln (__PRETTY_FUNCTION__); }

alias fntype = void function ();

fntype [string] routes = [
       "foo": &foo,
       "bar": &bar,
    ];

string usage (string pin)
{
    return format!"%s <action>" (pin);
}

int main (string [] args)
{
    enforce (args.length == 2, usage (args [0]));
    auto action = args [1];
    auto actfn = action in routes;
    enforce (actfn, format!"could not find route <%s>" (action));
    (*actfn) ();
    return 0;
}
```


More information about the Digitalmars-d mailing list