www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How do I create classes dynamically?

reply Mario <cbarch protonmail.com> writes:
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.
Apr 14 2021
next sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
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
Apr 14 2021
parent Mario <cbarch protonmail.com> writes:
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
Apr 14 2021
prev sibling next sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
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.
Apr 14 2021
prev sibling next sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Apr 15 2021
prev sibling next sibling parent Imperatorn <johan_forsberg_86 hotmail.com> writes:
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
Apr 15 2021
prev sibling next sibling parent reply Kagamin <spam here.lot> writes:
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; ```
Apr 15 2021
parent reply Imperatorn <johan_forsberg_86 hotmail.com> writes:
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: ```d mixin("class MyDynamicClassName { }"); MyDynamicClassName cls = new MyDynamicClassName; ```
Yes but not at runtime
Apr 15 2021
parent reply Jack <jckj33 gmail.com> writes:
On Thursday, 15 April 2021 at 17:48:02 UTC, Imperatorn wrote:
 On Thursday, 15 April 2021 at 16:39:30 UTC, Kagamin wrote:
 On Wednesday, 14 April 2021 at 20:38:16 UTC, Mario wrote:
 [...]
String mixins is D replacement of macros for code generation. Works like this: ```d mixin("class MyDynamicClassName { }"); MyDynamicClassName cls = new MyDynamicClassName; ```
Yes but not at runtime
Could you give an example of what you're trying to archive?
Apr 15 2021
parent reply Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Thursday, 15 April 2021 at 22:06:23 UTC, Jack wrote:
 On Thursday, 15 April 2021 at 17:48:02 UTC, Imperatorn wrote:
 On Thursday, 15 April 2021 at 16:39:30 UTC, Kagamin wrote:
 On Wednesday, 14 April 2021 at 20:38:16 UTC, Mario wrote:
 [...]
String mixins is D replacement of macros for code generation. Works like this: ```d mixin("class MyDynamicClassName { }"); MyDynamicClassName cls = new MyDynamicClassName; ```
Yes but not at runtime
Could you give an example of what you're trying to archive?
One example would be a repl
Apr 16 2021
parent reply Kagamin <spam here.lot> writes:
On Friday, 16 April 2021 at 08:31:27 UTC, Imperatorn wrote:
 One example would be a repl
That has little to do with what OP meant.
Apr 16 2021
parent Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Friday, 16 April 2021 at 18:42:35 UTC, Kagamin wrote:
 On Friday, 16 April 2021 at 08:31:27 UTC, Imperatorn wrote:
 One example would be a repl
That has little to do with what OP meant.
I didn't answer to that though
Apr 16 2021
prev sibling parent reply Martin <martin.brzenska googlemail.com> writes:
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.
Apr 15 2021
parent reply mw <mingwu gmail.com> writes:
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?
Apr 15 2021
next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Thu, Apr 15, 2021 at 08:56:18PM +0000, mw via Digitalmars-d-learn wrote:
[...]
 of course, one can manually dispatch:
 
 if      (userInputString == "cat") createCat();
 else if (userInputString == "dog") createDog();
 ...
 
 but this this tedious.
------------------- // Disclaimer: this is proof of concept, I didn't actually run this yet class Animal {} class Cat : Animal {} class Dog : Animal {} alias SupportedTypes = AliasSeq!(Cat, Dog, /* whatever else you want here */); string userInputString = ...; Animal result; SW: switch (userInputString) { static foreach (T; SupportedTypes) { case T.stringof: result = new T; break SW; } default: throw new Exception("Unknown object type"); } -------------------
 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.
Same idea: ------------------- // Disclaimer: this is proof of concept, I didn't actually run this yet struct Dispatcher { void bark() { ... } void meow() { ... } void moo() { ... } ... // whatever else you want here } Dispatcher disp; string userInputString = ...; SW: switch (userInputString) { static foreach (fieldName; __traits(allMembers, disp)) { static if (is(typeof(__traits(getMember, disp, fieldName)) == function) { case fieldName: __traits(getMember, disp, fieldName)(); break SW; } } } ------------------- Basically, the idea is to obtain a list of types/methods/whatever somehow (either by explicitly listing instances, or via compile-time introspection), then statically generate switch cases from it. You can eliminate many kinds of boilerplate using this little trick. T -- Береги платье снову, а здоровье смолоду.
Apr 15 2021
prev sibling next sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 4/15/21 1:56 PM, mw 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?
That's a different question because creating a class is different from creating instances (objects) of existing classes.
 again, one can manually dispatch, but is there a way to avoid this
 tediousness?
H. S. Teoh showed methods where lookup keys are known at compile time and are used to generate e.g. a switch statement. I have a system where the main logic of the program has no idea what types are out there. All it has is a lookup table. (Uncompiled pseude code follows.) struct Functions { void * function deserialize(ubyte[] bytes); void function process(void* input, void* output); void function serialize(ubyte[] bytes); } shared Functions[string] registration; So, the main logic uses user input to look up what the functions are: auto funcs = registration[userInput]; And applies those functions to some data at runtime. This is "dynamic" because registration is populated by the 'shared static this()' blocks of unknown modules that are loaded dynamically (and in my case conditionally): module foo; // This struct is what this module is about: struct Foo { // ... } // The module registers itself with the main lookup system when loaded: shared static this() { register("my lookup string", Functions(&deserializer!Foo, &processor!Foo, &serializer!Foo)); } Note that registered functions are template instances tailored for this specific type. However, the main logic has no klowledge of individual modules, not even the templates like 'deserializer': It only knows about a lookup table that contains some function pointers. I haven't used object creation functions above but that can be added to Functions as well. Ali
Apr 15 2021
prev sibling parent novice3 <sorryno em.ail> writes:
On Thursday, 15 April 2021 at 20:56:18 UTC, mw wrote:
 In response to user input?

 e.g. createObject(userInputString);
what if user enter "Cthulhu"? if you say "i will check user input", then you know list of possible inputs, then you can use some compile-time technics. if you dont know possible user inputs, and user can input anything, then questions raised: - what is object Cthulhu meaning? - what properties and methods it has? etc...
Apr 16 2021