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 assert_throw!AssertError( assert_eq(new Temp, 2) ); 113 assert_throw!AssertError( assert_eq(new Temp, 2) );
114 } 114 }
115 115
116 /* [Todo] is there any way to clearnly implement "assert_compiles" and "assert_n 116 /* [Todo] is there any way to clearnly implement "assert_compiles" and "assert_n
117 117
118 /// Mixing-in the bean constructor for a class 118 /// Mixing-in the bean constructor for a class
119 119
120 template SimpleConstructor() | 120 /*mixin*/ template SimpleConstructor()
121 { 121 {
122 static if( is(typeof(super) == Object) || super.tupleof.length==0 ) 122 static if( is(typeof(super) == Object) || super.tupleof.length==0 )
123 this( typeof(this.tupleof) params ) 123 this( typeof(this.tupleof) params )
124 { 124 {
125 static if(this.tupleof.length>0) 125 static if(this.tupleof.length>0)
126 this.tupleof = params; 126 this.tupleof = params;
127 } 127 }
................................................................................................................................................................................
169 mixin SimpleConstructor; 169 mixin SimpleConstructor;
170 } 170 }
171 }) ); 171 }) );
172 } 172 }
173 173
174 /// Mixing-in the MOST-DERIVED-member-wise comparator for a class 174 /// Mixing-in the MOST-DERIVED-member-wise comparator for a class
175 175
176 template SimpleCompare() | 176 /*mixin*/ template SimpleCompare()
177 { 177 {
178 override bool opEquals(Object rhs_) const 178 override bool opEquals(Object rhs_) const
179 { 179 {
180 if( auto rhs = cast(typeof(this))rhs_ ) 180 if( auto rhs = cast(typeof(this))rhs_ )
181 { 181 {
182 foreach(i,_; this.tupleof) 182 foreach(i,_; this.tupleof)
183 if( this.tupleof[i] != rhs.tupleof[i] ) 183 if( this.tupleof[i] != rhs.tupleof[i] )
................................................................................................................................................................................
231 string y; 231 string y;
232 mixin SimpleConstructor; 232 mixin SimpleConstructor;
233 mixin SimpleCompare; 233 mixin SimpleCompare;
234 } 234 }
235 assert_throw!AssertError( new Temp(1,"foo") == new TempDummy(1,"foo") ); 235 assert_throw!AssertError( new Temp(1,"foo") == new TempDummy(1,"foo") );
236 assert_throw!AssertError( new Temp(1,"foo") <= new TempDummy(1,"foo") ); 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 }