Initializing defaults based on type.

Artur Skawina via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Mar 6 13:29:24 PST 2015


On 03/06/15 19:27, Kagamin via Digitalmars-d-learn wrote:
> On Friday, 6 March 2015 at 16:39:56 UTC, Ali Çehreli wrote:
>> mixin (makePairInitValueDefinitions());
> 
> Oh, so that's how you do static foreach.

No, you implement it using CTFE magic, and then that code becomes:

   import std.typetuple;

   alias PairInitValues = TypeTuple!(
       int, 1,
       double, 0,
   );

   #foreach (N; 0..PairInitValues.length/2) {
      enum PairInitValue(T:PairInitValues[$N*2]) = PairInitValues[$N*2+1];
   }

   struct Pair(T)
   {
      T x = PairInitValue!T;
      T y = PairInitValue!T;

      alias x c;
      alias y r;
   }


Seriously though, avoid using `.stringof` when generating code - it will
break if the symbol is not available at the string-mixin scope.

artur 


More information about the Digitalmars-d-learn mailing list