www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Converting a POD struct to a class at compile-time ?

reply "Klb" <Klb somewhere.ut> writes:
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.
Jul 16 2014
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
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
Jul 16 2014
prev sibling next sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
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...
Jul 16 2014
parent "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
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...
Jul 16 2014
prev sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
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.
Jul 16 2014
next sibling parent reply "Klb" <Klb somewhere.ut> writes:
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...
Jul 16 2014
parent "Rene Zwanenburg" <renezwanenburg gmail.com> writes:
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
Jul 16 2014
prev sibling parent "Klb" <Klb somewhere.ut> writes:
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. :)
Jul 16 2014