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