@@ -5,11 +5,11 @@ * Common tricks and utilities for programming in D. */ module tricks.tricks; import tricks.test; -import std.array : appender; -import std.format : formattedWrite; -import core.exception; +import core.exception; +import std.array : appender; +import std.format : formattedWrite; import std.traits; import std.typetuple; /// Simple Wrapper for std.format.doFormat @@ -109,9 +109,9 @@ { if( auto rhs = cast(typeof(this))rhs_ ) { foreach(i,_; this.tupleof) - if( this.tupleof[i] != rhs.tupleof[i] ) + if( this.tupleof[i] != (cast(const)rhs).tupleof[i] ) return false; return true; } assert(false, sprintf!"Cannot compare %s with %s"(typeid(this), typeid(rhs_))); @@ -129,13 +129,14 @@ { if( auto rhs = cast(typeof(this))rhs_ ) { foreach(i,_; this.tupleof) - if( this.tupleof[i] != rhs.tupleof[i] ) { - auto a = this.tupleof[i]; + if( this.tupleof[i] != (cast(const)rhs).tupleof[i] ) { + auto a = (cast(SC_Unqual!(typeof(this)))this).tupleof[i]; auto b = rhs.tupleof[i]; - return cast(SC_Unqual!(typeof(a)))a < b ? -1 : +1; - } + return a < b ? -1 : +1; + } +// not work well for structures??????? // if(auto c = typeid(_).compare(&this.tupleof[i],&rhs.tupleof[i])) // return c; return 0; } @@ -322,9 +323,9 @@ string s; mixin SimpleConstructor; } class D3 : Base { - int[int]m; + int[int] m; mixin SimpleConstructor; } Base d1 = new D1(1, 2.3); @@ -333,61 +334,61 @@ // normal dispatch assert_eq( d1.match( (D1 x){return 1;}, - (D2 x){return 2;}, + (D2 x){return 2;} ), 1); assert_eq( d2.match( (D1 x){return 1;}, - (D2 x){return 2;}, + (D2 x){return 2;} ), 2); assert_throw!AssertError( d3.match( (D1 x){return 1;}, - (D2 x){return 2;}, + (D2 x){return 2;} )); assert_eq( d3.match( (D1 x){return 1;}, (D2 x){return 2;}, - (Base x){return 3;}, + (Base x){return 3;} ), 3); assert_eq( d2.match( (D1 x){return 1;}, (D2 x){return 2;}, - (Base x){return 3;}, + (Base x){return 3;} ), 2); assert_eq( d2.match( (D1 x){return 1;}, (Base x){return 3;}, - (D2 x){return 2;}, + (D2 x){return 2;} ), 3); // member decomposing match assert_eq( d1.match( when!(D1, (x, y){return x + cast(int)y;}), when!(D2, (x){return x.length;}), - when!(D3, (x){return x[1];}), + when!(D3, (x){return x[1];}) ), 3); assert_eq( d2.match( when!(D1, (x, y){return x + cast(int)y;}), when!(D2, (x){return x.length;}), - when!(D3, (x){return x[1];}), + when!(D3, (x){return x[1];}) ), 6); assert_eq( d3.match( when!(D1, (x, y){return x + cast(int)y;}), when!(D2, (x){return x.length;}), - when!(D3, (x){return x[1];}), + when!(D3, (x){return x[1];}) ), 10); assert_throw!AssertError( d3.match( when!(D1, (x, y){return x + cast(int)y;}), - when!(D2, (x){return x.length;}), + when!(D2, (x){return x.length;}) )); assert_eq( d2.match( when!(D1, (x, y){return x + cast(int)y;}), when!(D2, (x){return x.length;}), - otherwise!({return 999;}), + otherwise!({return 999;}) ), 6); assert_eq( d2.match( when!(D1, (x, y){return x + cast(int)y;}), otherwise!({return 999;}), - when!(D2, (x){return x.length;}), + when!(D2, (x){return x.length;}) ), 999); }