Differences From Artifact [e911a30d1f923777]:
- File        
polemy/tricks.d
- 2010-11-08 11:42:14 - part of checkin [5e407d7cf8] on branch trunk - Lexer Refactored so that it can accpet multi-symbol token (user: kinaba) [annotate]
 
 
To Artifact [fcea1bf69fccc37f]:
- File        
polemy/tricks.d
- 2010-11-08 11:57:48 - part of checkin [077506b38c] on branch trunk - Generic toString utility added. (user: kinaba) [annotate]
 
 
   113    113    assert_throw!AssertError( assert_eq(new Temp, 2) );
   114    114   }
   115    115   
   116    116   /* [Todo] is there any way to clearnly implement "assert_compiles" and "assert_not_compile"? */
   117    117   
   118    118   /// Mixing-in the bean constructor for a class
   119    119   
   120         -template SimpleConstructor()
          120  +/*mixin*/ template SimpleConstructor()
   121    121   {
   122    122    static if( is(typeof(super) == Object) || super.tupleof.length==0 )
   123    123     this( typeof(this.tupleof) params )
   124    124     {
   125    125      static if(this.tupleof.length>0)
   126    126       this.tupleof = params;
   127    127     }
................................................................................
   169    169      mixin SimpleConstructor;
   170    170     }
   171    171    }) );
   172    172   }
   173    173   
   174    174   /// Mixing-in the MOST-DERIVED-member-wise comparator for a class
   175    175   
   176         -template SimpleCompare()
          176  +/*mixin*/ template SimpleCompare()
   177    177   {
   178    178    override bool opEquals(Object rhs_) const
   179    179    {
   180    180     if( auto rhs = cast(typeof(this))rhs_ )
   181    181     {
   182    182      foreach(i,_; this.tupleof)
   183    183       if( this.tupleof[i] != rhs.tupleof[i] )
................................................................................
   231    231     string y;
   232    232     mixin SimpleConstructor;
   233    233     mixin SimpleCompare;
   234    234    }
   235    235    assert_throw!AssertError( new Temp(1,"foo") == new TempDummy(1,"foo") );
   236    236    assert_throw!AssertError( new Temp(1,"foo") <= new TempDummy(1,"foo") );
   237    237   }
          238  +
          239  +/// Mixing-in a simple toString method
          240  +
          241  +/*mixin*/ template SimpleToString()
          242  +{
          243  + override string toString()
          244  + {
          245  +  string str = sprintf!"%s("(typeof(this).stringof);
          246  +  foreach(i,mem; this.tupleof)
          247  +  {
          248  +   if(i) str ~= ",";
          249  +   static if( is(typeof(mem) == std.bigint.BigInt) )
          250  +    str ~= std.bigint.toDecimalString(mem);
          251  +   else
          252  +    str ~= sprintf!"%s"(mem);
          253  +  }
          254  +  return str ~ ")";
          255  + }
          256  +}
          257  +
          258  +version(unittest) import std.bigint;
          259  +unittest
          260  +{
          261  + class Temp
          262  + {
          263  +  int x;
          264  +  string y;
          265  +  BigInt z;
          266  +  mixin SimpleConstructor;
          267  +  mixin SimpleToString;
          268  + }
          269  + assert_eq( (new Temp(1,"foo",BigInt(42))).toString(), "Temp(1,foo,42)" );
          270  +}