Associative Arrays with elements having function type?
Paul Backus
snarwin at gmail.com
Mon Nov 3 22:54:15 UTC 2025
On Monday, 3 November 2025 at 21:29:47 UTC, kdevel wrote:
> 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,
> ];
In this example, `fntype` is not a "function type"; it is a
"function pointer type."
If you try to use an actual function type, it will fail:
```d
void foo () {}
void bar () {}
alias fntype = typeof(foo);
fntype [string] routes;
// Error: cannot have associative array of `void()`
```
More information about the Digitalmars-d
mailing list