Differences From Artifact [4c996bb5859ed5ea]:
- File
tricks/tricks.d
- 2010-11-09 06:02:32 - part of checkin [7de80acfb8] on branch trunk - Added ultra tenuki REPL (user: kinaba) [annotate]
To Artifact [d70e48b147acc242]:
- File
tricks/tricks.d
- 2010-11-10 12:38:54 - part of checkin [38fcc662be] on branch trunk - cleaned up documentation comments (user: kinaba) [annotate]
24 assert_eq( sprintf!"%s == %04d"("1+2", 3), "1+2 == 0003" ); 24 assert_eq( sprintf!"%s == %04d"("1+2", 3), "1+2 == 0003" );
25 assert_eq( sprintf!"%2$s == %1$s"("1+2", 5, 8), "5 == 1+2" ); 25 assert_eq( sprintf!"%2$s == %1$s"("1+2", 5, 8), "5 == 1+2" );
26 assert_throw!Error( sprintf!"%s%s"(1) ); 26 assert_throw!Error( sprintf!"%s%s"(1) );
27 } 27 }
28 28
29 /// Create an exception with automatically completed filename and lineno informa 29 /// Create an exception with automatically completed filename and lineno informa
30 30
31 auto genex(ExceptionType, string fn=__FILE__, int ln=__LINE__, T...)(T params) | 31 ExceptionType genex(ExceptionType, string fn=__FILE__, int ln=__LINE__, T...)(T
32 { 32 {
33 static if( T.length > 0 && is(T[$-1] : Throwable) ) 33 static if( T.length > 0 && is(T[$-1] : Throwable) )
34 return new ExceptionType(params[0..$-1], fn, ln, params[$-1]); 34 return new ExceptionType(params[0..$-1], fn, ln, params[$-1]);
35 else 35 else
36 return new ExceptionType(params, fn, ln); 36 return new ExceptionType(params, fn, ln);
37 } 37 }
38 38
................................................................................................................................................................................
44 } 44 }
45 45
46 /// Mixing-in the bean constructor for a class 46 /// Mixing-in the bean constructor for a class
47 47
48 /*mixin*/ 48 /*mixin*/
49 template SimpleConstructor() 49 template SimpleConstructor()
50 { 50 {
> 51 /// member-by-member constructor
51 static if( is(typeof(super) == Object) || super.tupleof.length==0 ) 52 static if( is(typeof(super) == Object) || super.tupleof.length==0 )
52 this( typeof(this.tupleof) params ) 53 this( typeof(this.tupleof) params )
53 { 54 {
54 static if(this.tupleof.length>0) 55 static if(this.tupleof.length>0)
55 this.tupleof = params; 56 this.tupleof = params;
56 } 57 }
57 else 58 else
................................................................................................................................................................................
89 assert_eq( (new Tomp(1,"foo",2.5)).z, 2.5 ); 90 assert_eq( (new Tomp(1,"foo",2.5)).z, 2.5 );
90 assert( !__traits(compiles, new Tomp(3.14)) ); 91 assert( !__traits(compiles, new Tomp(3.14)) );
91 92
92 // shiyo- desu. Don't use in this way. 93 // shiyo- desu. Don't use in this way.
93 // Tamp tries to call new Tomp(real) (because it only sees Tomp's memb 94 // Tamp tries to call new Tomp(real) (because it only sees Tomp's memb
94 // but it fails because Tomp takes (int,string,real). 95 // but it fails because Tomp takes (int,string,real).
95 assert( !__traits(compiles, { 96 assert( !__traits(compiles, {
96 class Tamp : Tomp <
97 { <
98 mixin SimpleConstructor; | 97 class Tamp : Tomp { mixin SimpleConstructor; }
99 } <
100 }) ); 98 }) );
101 } 99 }
102 100
103 /// Mixing-in the MOST-DERIVED-member-wise comparator for a class 101 /// Mixing-in the MOST-DERIVED-member-wise comparator for a class
104 102
105 /*mixin*/ 103 /*mixin*/
106 template SimpleCompare() 104 template SimpleCompare()
107 { 105 {
108 override bool opEquals(Object rhs_) const | 106 override bool opEquals(Object rhs_) const /// member-by-member equality
109 { 107 {
110 if( auto rhs = cast(typeof(this))rhs_ ) 108 if( auto rhs = cast(typeof(this))rhs_ )
111 { 109 {
112 foreach(i,_; this.tupleof) 110 foreach(i,_; this.tupleof)
113 if( this.tupleof[i] != rhs.tupleof[i] ) 111 if( this.tupleof[i] != rhs.tupleof[i] )
114 return false; 112 return false;
115 return true; 113 return true;
116 } 114 }
117 assert(false, sprintf!"Cannot compare %s with %s"(typeid(this), 115 assert(false, sprintf!"Cannot compare %s with %s"(typeid(this),
118 } 116 }
119 117
120 override hash_t toHash() const | 118 override hash_t toHash() const /// member-by-member hash
121 { 119 {
122 hash_t h = 0; 120 hash_t h = 0;
123 foreach(mem; this.tupleof) 121 foreach(mem; this.tupleof)
124 h += typeid(mem).getHash(&mem); 122 h += typeid(mem).getHash(&mem);
125 return h; 123 return h;
126 } 124 }
127 125
128 override int opCmp(Object rhs_) const | 126 override int opCmp(Object rhs_) const /// member-by-member compare
129 { 127 {
130 if( auto rhs = cast(typeof(this))rhs_ ) 128 if( auto rhs = cast(typeof(this))rhs_ )
131 { 129 {
132 foreach(i,_; this.tupleof) 130 foreach(i,_; this.tupleof)
133 if(auto c = typeid(_).compare(&this.tupleof[i],& 131 if(auto c = typeid(_).compare(&this.tupleof[i],&
134 return c; 132 return c;
135 return 0; 133 return 0;
................................................................................................................................................................................
167 } 165 }
168 166
169 /// Mixing-in a simple toString method 167 /// Mixing-in a simple toString method
170 168
171 /*mixin*/ 169 /*mixin*/
172 template SimpleToString() 170 template SimpleToString()
173 { 171 {
> 172 /// member-by-member toString
174 override string toString() 173 override string toString()
175 { 174 {
176 string str = sprintf!"%s("(typeof(this).stringof); 175 string str = sprintf!"%s("(typeof(this).stringof);
177 foreach(i,mem; this.tupleof) 176 foreach(i,mem; this.tupleof)
178 { 177 {
179 if(i) str ~= ","; 178 if(i) str ~= ",";
180 static if( is(typeof(mem) == std.bigint.BigInt) ) 179 static if( is(typeof(mem) == std.bigint.BigInt) )