Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
July 16, 2014 Converting a POD struct to a class at compile-time ? | ||||
---|---|---|---|---|
| ||||
Hello, I'd like to know if it's possible, using CTFE, mixin etc to convert a POD struct to a class at compile time. I've reached the step where the string to mix-in is generated but I cant mix it: ------------------------------------------------------------- import std.stdio; import std.traits, std.typetuple; static private template genClassFromStruct(S) if (is(S == struct) &(__traits(isPOD, S))) { auto values = S.init.tupleof; auto types = typeid(typeof(values)); auto names = __traits(allMembers, S); string genClassFromStruct() { string members; foreach(int i,t; RepresentationTypeTuple!S) { members ~= t.stringof ~ " " ~ names[i] ~ ";\r"; } return "class c" ~ S.stringof ~ "{" ~ ";\r"~ members ~ "}"; } } struct foo{ int a; float b;} //mixin( genClassFromStruct!foo ); void main(string args[]) { foo Foo; //auto c = new cfoo; writeln( genClassFromStruct!foo ); } ------------------------------------------------------------- The problem appends when un-commenting the mixin: Error: static variable _names_field_0 cannot be read at compile time. |
July 16, 2014 Re: Converting a POD struct to a class at compile-time ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Klb | Klb:
> Hello, I'd like to know if it's possible, using CTFE, mixin etc to convert a POD struct to a class at compile time.
I remember Andrei planned to add this small thingie to Phobos, but it's still missing.
Bye,
bearophile
|
July 16, 2014 Re: Converting a POD struct to a class at compile-time ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Klb | Not a direct answer, but the way I'd do this is to just use composition: class Foo { YourStruct _this; alias _this this; } boom, it'd work pretty well just like that... |
July 16, 2014 Re: Converting a POD struct to a class at compile-time ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Klb | On Wednesday, 16 July 2014 at 17:43:03 UTC, Klb wrote: > auto names = __traits(allMembers, S); > Error: static variable _names_field_0 cannot be read at compile time. The problem there is names is a regular runtime variable and mixins need to use compile time stuff. If you make that enum instead of auto, it'll probably work. |
July 16, 2014 Re: Converting a POD struct to a class at compile-time ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Wed, Jul 16, 2014 at 06:07:32PM +0000, Adam D. Ruppe via Digitalmars-d-learn wrote: > Not a direct answer, but the way I'd do this is to just use composition: > > class Foo { > YourStruct _this; > alias _this this; > } > > > boom, it'd work pretty well just like that... +1, simple answer to simple question, and `alias this` totally rawks. :-) T -- Famous last words: I *think* this will work... |
July 16, 2014 Re: Converting a POD struct to a class at compile-time ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Wednesday, 16 July 2014 at 18:09:10 UTC, Adam D. Ruppe wrote:
> On Wednesday, 16 July 2014 at 17:43:03 UTC, Klb wrote:
>
>> auto names = __traits(allMembers, S);
>
>> Error: static variable _names_field_0 cannot be read at compile time.
>
> The problem there is names is a regular runtime variable and mixins need to use compile time stuff. If you make that enum instead of auto, it'll probably work.
Unfortunately I can't encapsulate and alias. Behind the question stands another idea: I do something with an interface implementer, as the "something" is not compat. with structs the idea was to generate a class as a string, with the interface and its default method...
|
July 16, 2014 Re: Converting a POD struct to a class at compile-time ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Wednesday, 16 July 2014 at 18:09:10 UTC, Adam D. Ruppe wrote:
> On Wednesday, 16 July 2014 at 17:43:03 UTC, Klb wrote:
>
>> auto names = __traits(allMembers, S);
>
>> Error: static variable _names_field_0 cannot be read at compile time.
>
> The problem there is names is a regular runtime variable and mixins need to use compile time stuff. If you make that enum instead of auto, it'll probably work.
Nice ! It works. :)
|
July 16, 2014 Re: Converting a POD struct to a class at compile-time ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Klb | On Wednesday, 16 July 2014 at 18:27:31 UTC, Klb wrote: > On Wednesday, 16 July 2014 at 18:09:10 UTC, Adam D. Ruppe wrote: >> On Wednesday, 16 July 2014 at 17:43:03 UTC, Klb wrote: >> >>> auto names = __traits(allMembers, S); >> >>> Error: static variable _names_field_0 cannot be read at compile time. >> >> The problem there is names is a regular runtime variable and mixins need to use compile time stuff. If you make that enum instead of auto, it'll probably work. > > Unfortunately I can't encapsulate and alias. Behind the question stands another idea: I do something with an interface implementer, as the "something" is not compat. with structs the idea was to generate a class as a string, with the interface and its default method... If I understand you correctly, 'wrap' may be what you're looking for: http://dlang.org/library/std/typecons/wrap.html |
Copyright © 1999-2021 by the D Language Foundation