digitalmars.D.learn - Dynamic Ctors ?
- Voitech (22/22) Feb 06 2016 Hi, i have a variadic args template, with a class inside
- Voitech (3/26) Feb 06 2016 This gives Error: memcpy cannot be interpreted at compile time,
- =?UTF-8?Q?Ali_=c3=87ehreli?= (34/36) Feb 06 2016 You can use string mixins (makeCtor and makeCtors):
- Voitech (3/9) Feb 07 2016 Thank you very much for answering.
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
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 workThis gives Error: memcpy cannot be interpreted at compile time, because it has no available source code
Feb 06 2016
On 02/06/2016 10:05 AM, Voitech wrote:create manually, constructor for each of T... parameter typesYou 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
On Saturday, 6 February 2016 at 23:35:17 UTC, Ali Çehreli wrote:On 02/06/2016 10:05 AM, Voitech wrote:Thank you very much for answering. Cheers[...]You can use string mixins (makeCtor and makeCtors): string makeCtor(T)() { import std.string : format; [...]
Feb 07 2016
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: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; // ????? } } }On 02/06/2016 10:05 AM, Voitech wrote:Thank you very much for answering. Cheers[...]You can use string mixins (makeCtor and makeCtors): string makeCtor(T)() { import std.string : format; [...]
Feb 08 2016
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
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