How do I create classes dynamically?

Kagamin spam at here.lot
Thu Apr 15 16:39:30 UTC 2021


On Wednesday, 14 April 2021 at 20:38:16 UTC, Mario wrote:
> Maybe I am just too short in D, but I wanted to find out if it 
> is possible to create classes dynamically. My problem is, I 
> just don't know where to start reading. Maybe at mixin 
> templates?
>
> CreateClassWithName!("MyDynamicClassName");
>
> should create the following class to work with dynamically:
>
> class MyDynamicClassName {
>     this() { writeln("I was not written, but still I exist!"); }
> }
>
> So that I in the end by means of
>
> MyDynamicClassName cls = new MyDynamicClassName;

String mixins is D replacement of macros for code generation.
Works like this:
```d
mixin("class MyDynamicClassName { }");
MyDynamicClassName cls = new MyDynamicClassName;
```


More information about the Digitalmars-d-learn mailing list