template this parameters for constructors

Igor Stepanov wazar.leollone at yahoo.com
Wed Feb 20 09:11:46 PST 2013


What about template this for compile-time constructing of structs 
(classes?)?
e.g.
struct BigDecimal
{
   this(string arg)
   {
     *this = str2Dec(arg);
   }
   template this(string arg)
   {
     enum this = str2Dec(arg);
   }

   static BigDecimal str2Dec(string arg) //can be evaluated at 
compile time
   {
     //...
   }
}

void main()
{
   string str = "42.24";
   BigDecimal num1 = str;//Calling constructor BigDecimal.this
   BigDecimal num2 = "3,1415926535 8979323846 2643383279 
5028841971 6939937510 5820974944 5923078164 0628620899 8628034825 
3421170679 8214808651 3282306647 0938446095 5058223172 5359408128 
4811174502 8410270193 8521105559 6446229489 5493038196 4428810975 
6659334461 2847564823 3786783165 2712019091 4564856692 3460348610 
4543266482 1339360726 0249141273 7245870066 0631558817 4881520920 
9628292540 9171536436 7892590360 0113305305 4882046652 1384146951 
9415116094 3305727036 5759591953 0921861173 8193261179"; 
//Calling template this at compile time.
}



More information about the Digitalmars-d mailing list