digitalmars.D.learn - Converting a POD struct to a class at compile-time ?
- Klb (39/39) Jul 16 2014 Hello, I'd like to know if it's possible, using CTFE, mixin etc
- bearophile (5/7) Jul 16 2014 I remember Andrei planned to add this small thingie to Phobos,
- Adam D. Ruppe (7/7) Jul 16 2014 Not a direct answer, but the way I'd do this is to just use
- H. S. Teoh via Digitalmars-d-learn (6/16) Jul 16 2014 +1, simple answer to simple question, and `alias this` totally rawks.
- Adam D. Ruppe (4/7) Jul 16 2014 The problem there is names is a regular runtime variable and
- Klb (6/13) Jul 16 2014 Unfortunately I can't encapsulate and alias. Behind the question
- Rene Zwanenburg (4/20) Jul 16 2014 If I understand you correctly, 'wrap' may be what you're looking
- Klb (2/9) Jul 16 2014 Nice ! It works. :)
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
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
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
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
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
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: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...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
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:If I understand you correctly, 'wrap' may be what you're looking for: http://dlang.org/library/std/typecons/wrap.htmlOn Wednesday, 16 July 2014 at 17:43:03 UTC, Klb wrote: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...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
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:Nice ! It works. :)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