digitalmars.D.learn - How do I create classes dynamically?
- Mario (16/16) Apr 14 2021 Maybe I am just too short in D, but I wanted to find out if it is
- Steven Schveighoffer (7/30) Apr 14 2021 There is no good supported way to do this with the current runtime.
- Mario (5/13) Apr 14 2021 Too bad. Nevertheless, thank you for your quick answer to my
- Adam D. Ruppe (4/8) Apr 14 2021 What exactly do you mean?
- =?UTF-8?Q?Ali_=c3=87ehreli?= (13/18) Apr 15 2021 Both mixins and templates are compile time features.
- Imperatorn (4/21) Apr 15 2021 ldc.JIT presumably has smth like that
- Kagamin (7/18) Apr 15 2021 String mixins is D replacement of macros for code generation.
- Imperatorn (2/26) Apr 15 2021 Yes but not at runtime
- Jack (2/13) Apr 15 2021 Could you give an example of what you're trying to archive?
- Imperatorn (2/16) Apr 16 2021 One example would be a repl
- Kagamin (2/3) Apr 16 2021 That has little to do with what OP meant.
- Imperatorn (2/5) Apr 16 2021 I didn't answer to that though
- Martin (3/5) Apr 15 2021 out of curiosity: Why you would like to do this? I cannot think
- mw (14/19) Apr 15 2021 In response to user input?
- H. S. Teoh (50/59) Apr 15 2021 -------------------
- =?UTF-8?Q?Ali_=c3=87ehreli?= (39/46) Apr 15 2021 That's a different question because creating a class is different from
- novice3 (11/13) Apr 16 2021 what if user enter "Cthulhu"?
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
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
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. -SteveToo bad. Nevertheless, thank you for your quick answer to my question. -Mario
Apr 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.
Apr 14 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 firstIf 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
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
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
On Thursday, 15 April 2021 at 16:39:30 UTC, Kagamin wrote:On Wednesday, 14 April 2021 at 20:38:16 UTC, Mario wrote:Yes but not at runtimeMaybe 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
On Thursday, 15 April 2021 at 17:48:02 UTC, Imperatorn wrote:On Thursday, 15 April 2021 at 16:39:30 UTC, Kagamin wrote:Could you give an example of what you're trying to archive?On Wednesday, 14 April 2021 at 20:38:16 UTC, Mario wrote:Yes but not at runtime[...]String mixins is D replacement of macros for code generation. Works like this: ```d mixin("class MyDynamicClassName { }"); MyDynamicClassName cls = new MyDynamicClassName; ```
Apr 15 2021
On Thursday, 15 April 2021 at 22:06:23 UTC, Jack wrote:On Thursday, 15 April 2021 at 17:48:02 UTC, Imperatorn wrote:One example would be a replOn Thursday, 15 April 2021 at 16:39:30 UTC, Kagamin wrote:Could you give an example of what you're trying to archive?On Wednesday, 14 April 2021 at 20:38:16 UTC, Mario wrote:Yes but not at runtime[...]String mixins is D replacement of macros for code generation. Works like this: ```d mixin("class MyDynamicClassName { }"); MyDynamicClassName cls = new MyDynamicClassName; ```
Apr 16 2021
On Friday, 16 April 2021 at 08:31:27 UTC, Imperatorn wrote:One example would be a replThat has little to do with what OP meant.
Apr 16 2021
On Friday, 16 April 2021 at 18:42:35 UTC, Kagamin wrote:On Friday, 16 April 2021 at 08:31:27 UTC, Imperatorn wrote:I didn't answer to that thoughOne example would be a replThat has little to do with what OP meant.
Apr 16 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.
Apr 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: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?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
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
On 4/15/21 1:56 PM, mw wrote:That's a different question because creating a class is different from creating instances (objects) of existing classes.In response to user input?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.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
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