www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Dynamic Ctors ?

reply Voitech <woipoi gmail.com> writes:
Hi, i have a variadic args template, with a class inside 
something like:

template foo(T...){


class Inner(){

...
...
}



}

Now i want to make Inner create or i will create manually, 
constructor for each of T... parameter types, but don't know what 
is syntax for it. I found that there is possibility to initialize 
T... types static variables with AliasSeq!(T) but how to do this 
automatically, or with static ifs,for constructors ?
I tried ...

template foo(T...){
alias Types=AliasSeq!(T);
Algebraic!(T) value;
this(Types value)
this.value=value;
}
}
But this dosn't work
Feb 06 2016
next sibling parent Voitech <woipoi gmail.com> writes:
On Saturday, 6 February 2016 at 18:05:05 UTC, Voitech wrote:
 Hi, i have a variadic args template, with a class inside 
 something like:

 template foo(T...){


 class Inner(){

 ...
 ...
 }



 }

 Now i want to make Inner create or i will create manually, 
 constructor for each of T... parameter types, but don't know 
 what is syntax for it. I found that there is possibility to 
 initialize T... types static variables with AliasSeq!(T) but 
 how to do this automatically, or with static ifs,for 
 constructors ?
 I tried ...

 template foo(T...){
 alias Types=AliasSeq!(T);
 Algebraic!(T) value;
 this(Types value)
 this.value=value;
 }
 }
 But this dosn't work
This gives Error: memcpy cannot be interpreted at compile time, because it has no available source code
Feb 06 2016
prev sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 02/06/2016 10:05 AM, Voitech wrote:

 create manually, constructor
 for each of T... parameter types
You can use string mixins (makeCtor and makeCtors): string makeCtor(T)() { import std.string : format; return format(q{ this (%s t) { import std.stdio : writefln; writefln("Ctor for %%s", %s.stringof); } }, T.stringof, T.stringof); } // Note: Use a pragma(msg) to see what it generates: // pragma(msg, makeCtor!int); string makeCtors(T...)() { string result; foreach (Type; T) { result ~= makeCtor!Type(); } return result; } mixin template foo(T...) { class Inner { mixin (makeCtors!T()); } } mixin foo!(int, string); void main() { auto i = new Inner(42); auto s = new Inner("hello"); } Output: Ctor for int Ctor for string Ali
Feb 06 2016
parent reply Voitech <woipoi gmail.com> writes:
On Saturday, 6 February 2016 at 23:35:17 UTC, Ali Çehreli wrote:
 On 02/06/2016 10:05 AM, Voitech wrote:

 [...]
You can use string mixins (makeCtor and makeCtors): string makeCtor(T)() { import std.string : format; [...]
Thank you very much for answering. Cheers
Feb 07 2016
parent reply Voitech <woipoi gmail.com> writes:
On Monday, 8 February 2016 at 07:08:58 UTC, Voitech wrote:
 On Saturday, 6 February 2016 at 23:35:17 UTC, Ali Çehreli wrote:
 On 02/06/2016 10:05 AM, Voitech wrote:

 [...]
You can use string mixins (makeCtor and makeCtors): string makeCtor(T)() { import std.string : format; [...]
Thank you very much for answering. Cheers
Unfortunately this doesn't work as I'am trying to assign value from ctor to member something like: this(%s value){ this.value=value; } it gives me Error: memcpy cannot be interpreted at compile time, because it has no available source code. Probably this is caused by declaration of member which is also parametrized with T... Member is: Algebraic!(T) value; So declaration of this mixin constructors are done before variable value is declared (compilation)? Is there any field in class that would contain ctors ? Something like __ctors ? i could try to init this(T... value) constructors in static constructor using delegates/functions. Something like: template Outer(T...){ class Inner{ Algebraic!(T) value; alias ctor(Type) =Inner delegate (Type value); static this(){ foreach(Type;T){ ctor!(Type) c={ this.value=value; }; __ctors~=c; // ????? } } }
Feb 08 2016
parent reply Kagamin <spam here.lot> writes:
http://dpaste.dzfl.pl/1f25ac34c1ee
You need Tuple, not Algebraic. Algebraic stores only one value of 
one type from a set, like Variant.
Feb 08 2016
parent Voitech <woipoi gmail.com> writes:
On Monday, 8 February 2016 at 15:09:30 UTC, Kagamin wrote:
 http://dpaste.dzfl.pl/1f25ac34c1ee
 You need Tuple, not Algebraic. Algebraic stores only one value 
 of one type from a set, like Variant.
Thank you for answering. You right if i would want to store all types of T.. in an Inner instance. But my conception was bad from the beginning. I want to store only one type of value in Inner instance. I just wanted to create generic constructor for each of Type which is invalid, cause instance holds value of single type that's why it is not parametrized by itself.
Feb 08 2016