Call function by its string name

aldanor i.s.smirnov at gmail.com
Sat Oct 19 10:17:46 PDT 2013


On Saturday, 19 October 2013 at 16:56:50 UTC, John Colvin wrote:
> On Saturday, 19 October 2013 at 16:33:11 UTC, aldanor wrote:
>> I was wondering if it was possible to call D functions by 
>> their names (strings that are not known at compile time) and 
>> couldn't find the answer anywhere in the documentation. Kinda 
>> like we can instantiate objects with Object.factory, would it 
>> be possible to somehow do the same with module-level 
>> functions? Or maybe with non-static class methods?
>
> You could make an associative array of function pointers with 
> strings as keys. Probably not the best solution but it should 
> work.

Thanks for the reply, this is something I thought about of 
course. But what if I have hundreds of functions to dispatch to? 
The current (C) implementation does exactly that, autogenerates a 
sort of an associative array, but that's very ugly and requires 
an extra preprocessing step.

I was thinking runtime reflections can help do this, but I'm not 
quite sure where to start (and there is also the performance 
question).


More information about the Digitalmars-d mailing list