Thread overview | |||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
September 06, 2015 Is D suitable for my latest project? | ||||
---|---|---|---|---|
| ||||
Hi All, I am considering using D for my latest project and there are a few features I would like and am not entirely sure at this point whether D has them. They are: - dynamic creation of classes/structs at runtime (think I can emulate this with variants/dynamic) - dynamic compilation of code files at runtime - some basic code creation tools Are the above possible? Thanks for any help, Chris Stevens |
September 06, 2015 Re: Is D suitable for my latest project? | ||||
---|---|---|---|---|
| ||||
Posted in reply to chris stevens | On Sunday, 6 September 2015 at 14:36:53 UTC, chris stevens wrote:
> - dynamic compilation of code files at runtime
I guess I could just invoke the compiler from my code for this? I would also like to be able to load this compiled code into the current process. This probably can be achieved using a simple loadLibrary command right?
|
September 06, 2015 Re: Is D suitable for my latest project? | ||||
---|---|---|---|---|
| ||||
Posted in reply to chris stevens | On Sunday, 6 September 2015 at 14:36:53 UTC, chris stevens wrote: > - dynamic creation of classes/structs at runtime. You have Object.factory for this. You can also use a custom factory based on string comparison. (with some: static if(condition) return new This; else static if(otherCondition) return new That; etc). > - dynamic compilation of code files at runtime use std.process to call a compiler. > - some basic code creation tools if you mean to generate code as string, writing them to a file, of course it will work in D. |
September 06, 2015 Re: Is D suitable for my latest project? | ||||
---|---|---|---|---|
| ||||
Posted in reply to BBasile | Thanks so much for your reply.
On Sunday, 6 September 2015 at 14:45:45 UTC, BBasile wrote:
> if you mean to generate code as string, writing them to a file, of course it will work in D.
I guess you're right it wouldn't be too difficult to do it all using strings. The code generation I'd done before in c# I'd used some 3rd person library where you build up an object model rather than using strings.
Ok, all sounds good. Looks like I'm going to be coding in D.:)
|
September 07, 2015 Re: Is D suitable for my latest project? | ||||
---|---|---|---|---|
| ||||
Posted in reply to chris stevens | On Sunday, 6 September 2015 at 15:15:03 UTC, chris stevens wrote:
> I guess you're right it wouldn't be too difficult to do it all using strings. The code generation I'd done before in c# I'd used some 3rd person library where you build up an object model rather than using strings.
Maybe with dparse you can construct an AST and later convert it to string.
|
September 08, 2015 Re: Is D suitable for my latest project? | ||||
---|---|---|---|---|
| ||||
Posted in reply to BBasile | On Sunday, 6 September 2015 at 14:45:45 UTC, BBasile wrote:
> You have Object.factory for this. You can also use a custom factory based on string comparison. (with some: static if(condition) return new This; else static if(otherCondition) return new That; etc).
I just had a look at Object.factory and this isn't actually what I wanted. I was looking for something that would allow me to create new (previously undefined) classes in D at runtime that I could then use with Object.factory to create instances of.
I think I can do this with Variants and dynamic, is this possible? Or is there another way?
|
September 08, 2015 Re: Is D suitable for my latest project? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kagamin | On Monday, 7 September 2015 at 07:57:07 UTC, Kagamin wrote:
> On Sunday, 6 September 2015 at 15:15:03 UTC, chris stevens wrote:
>> I guess you're right it wouldn't be too difficult to do it all using strings. The code generation I'd done before in c# I'd used some 3rd person library where you build up an object model rather than using strings.
>
> Maybe with dparse you can construct an AST and later convert it to string.
Thanks for the reply Kagamin, just had a quick look at dparse and not sure how I could use it in the way you describe. Any examples of this?
|
September 08, 2015 Re: Is D suitable for my latest project? | ||||
---|---|---|---|---|
| ||||
Posted in reply to chris stevens | On Tuesday, 8 September 2015 at 19:30:16 UTC, chris stevens wrote:
> On Sunday, 6 September 2015 at 14:45:45 UTC, BBasile wrote:
>> You have Object.factory for this. You can also use a custom factory based on string comparison. (with some: static if(condition) return new This; else static if(otherCondition) return new That; etc).
>
> I just had a look at Object.factory and this isn't actually what I wanted. I was looking for something that would allow me to create new (previously undefined) classes in D at runtime that I could then use with Object.factory to create instances of.
>
> I think I can do this with Variants and dynamic, is this possible? Or is there another way?
"Previously undefined". As far as I know, this is impossible in D. Thr compiler has to know how much memory to allocate/request and it has to know that at compiletime (else it wouldn't be the compiler!)
This functionality in Java/C# is possible because it's running on a vm that allows special instructions to make the vm compile and make available a class dynamically (and by extension, slowly)
|
September 08, 2015 Re: Is D suitable for my latest project? | ||||
---|---|---|---|---|
| ||||
Posted in reply to chris stevens | On Tuesday, 8 September 2015 at 19:30:16 UTC, chris stevens wrote: > On Sunday, 6 September 2015 at 14:45:45 UTC, BBasile wrote: >> You have Object.factory for this. You can also use a custom factory based on string comparison. (with some: static if(condition) return new This; else static if(otherCondition) return new That; etc). > > I just had a look at Object.factory and this isn't actually what I wanted. I was looking for something that would allow me to create new (previously undefined) classes in D at runtime that I could then use with Object.factory to create instances of. > > I think I can do this with Variants and dynamic, is this possible? Or is there another way? No, it's not possible with variants. What you want to do is actually complex and won't be solved here. To create a new class instance, the runtime needs the TypeInfo class for the class type. Why ? because for example if in a class declaration you declare an int initially equal to 1, the TypeInfo class provide the memory layout that matches to this initital value. For example this is how class instances are creates in D: https://github.com/D-Programming-Language/druntime/blob/master/src/rt/lifetime.d#L71 --- extern (C) Object _d_newclass(const ClassInfo ci) --- without the 'ClassInfo' parameter, you can't get the initial state of a class. This is what the runtime needs to create a class instance. |
September 09, 2015 Re: Is D suitable for my latest project? | ||||
---|---|---|---|---|
| ||||
Posted in reply to chris stevens | On Tuesday, 8 September 2015 at 20:01:15 UTC, chris stevens wrote:
> Thanks for the reply Kagamin, just had a quick look at dparse and not sure how I could use it in the way you describe. Any examples of this?
As I understand, you wanted to build an AST tree and format it to string?
|
Copyright © 1999-2021 by the D Language Foundation