Jump to page: 1 2
Thread overview
How do I create classes dynamically?
Apr 14, 2021
Mario
Apr 14, 2021
Mario
Apr 14, 2021
Adam D. Ruppe
Apr 15, 2021
Ali Çehreli
Apr 15, 2021
Imperatorn
Apr 15, 2021
Kagamin
Apr 15, 2021
Imperatorn
Apr 15, 2021
Jack
Apr 16, 2021
Imperatorn
Apr 16, 2021
Kagamin
Apr 16, 2021
Imperatorn
Apr 15, 2021
Martin
Apr 15, 2021
mw
Apr 15, 2021
H. S. Teoh
Apr 15, 2021
Ali Çehreli
Apr 16, 2021
novice3
April 14, 2021

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;

can work with it.

Normally I would think of a macro first, but as far as I understood D doesn't know macros (which I'm not really sad about), but maybe I'm just thinking too complicated yet. I would appreciate any hints, because as I said, I don't even know where to start reading.

April 14, 2021
On 4/14/21 4:38 PM, 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;
> 
> can work with it.
> 
> Normally I would think of a macro first, but as far as I understood D doesn't know macros (which I'm not really sad about), but maybe I'm just thinking too complicated yet. I would appreciate any hints, because as I said, I don't even know where to start reading.
> 

There is no good supported way to do this with the current runtime.

The way this is normally done is to use your own reflection system, and build a mechanism to create classes based on their name.

There was an old way, using Object.factory, but I would not recommend that, it's brittle and doesn't always work.

-Steve
April 14, 2021
On Wednesday, 14 April 2021 at 20:47:57 UTC, Steven Schveighoffer wrote:
> There is no good supported way to do this with the current runtime.
>
> The way this is normally done is to use your own reflection system, and build a mechanism to create classes based on their name.
>
> There was an old way, using Object.factory, but I would not recommend that, it's brittle and doesn't always work.
>
> -Steve

Too bad. Nevertheless, thank you for your quick answer to my question.

-Mario
April 14, 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?

What exactly do you mean?

Your goal is probably achievable through some means, like I can kinda make D classes with a scripting language even.

April 15, 2021
On 4/14/21 1:38 PM, Mario wrote:

> Maybe I am just too short in D, but I wanted to find out if it is
> possible to create classes dynamically.

In D world, "dynamically" means "at run time".

> Maybe at mixin templates?

Both mixins and templates are compile time features.

> Normally I would think of a macro first

If we are talking about C (and C++) macros, they are compile time features as well.

So, I think you want help from D to generate types, which can happen only at compile time with statically-typed languages like D. And yes, D is a great language for "generative programming" like that.

> "I was not written, but still I exist!"

Not possible by this "programming language", which wants a source code written, by a human or a machine, to be compiled. Still, anything is possible: For example, you can generate source code, dispatch a compiler, and load dynamically at run time. :)

Ali

April 15, 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;

can work with it.

Normally I would think of a macro first, but as far as I understood D doesn't know macros (which I'm not really sad about), but maybe I'm just thinking too complicated yet. I would appreciate any hints, because as I said, I don't even know where to start reading.

ldc.JIT presumably has smth like that

https://gist.github.com/eldar/2294388

Also look up ldc.dynamic_compile

April 15, 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:

mixin("class MyDynamicClassName { }");
MyDynamicClassName cls = new MyDynamicClassName;
April 15, 2021

On Thursday, 15 April 2021 at 16:39:30 UTC, Kagamin wrote:

>

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:

mixin("class MyDynamicClassName { }");
MyDynamicClassName cls = new MyDynamicClassName;

Yes but not at runtime

April 15, 2021

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.

April 15, 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?

« First   ‹ Prev
1 2