How do I create classes dynamically?

mw mingwu at gmail.com
Thu Apr 15 20:56:18 UTC 2021


On Thursday, 15 April 2021 at 18:21:16 UTC, Martin wrote:
> On Wednesday, 14 April 2021 at 20:38:16 UTC, Mario wrote:
>> I wanted to find out if it is possible to create classes 
>> dynamically.
>
> out of curiosity: Why you would like to do this? I cannot think 
> of a use case for this - this is why i ask.

In response to user input?

e.g. createObject(userInputString);

of course, one can manually dispatch:

if      (userInputString == "cat") createCat();
else if (userInputString == "dog") createDog();
...

but this this tedious.


I have a similar question: how to dynamically use user's input 
string as function name can call it? suppose the function has no 
argument.

callFunc(userInputString)();  // call that func.

again, one can manually dispatch, but is there a way to avoid 
this tediousness?





More information about the Digitalmars-d-learn mailing list