Compares two things together
Compares two things together by comparing a common publicly visible field of T and U.
1 // single elements 2 assert(!equal(2, 4)); 3 4 // unary predicate function applied to elements then compared 5 assert( equal!(a => a % 2 == 0)(2, 4)); 6 assert( equal!q{a % 2 == 0}(2, 4)); 7 8 // binary predicate used to compare elements 9 assert(!equal!((a, b) => a == b)(2, 4)); 10 assert( equal!q{a != b}(2, 4)); 11 12 // compare ranges of ranges of different range types but same value types 13 import std.algorithm: map, filter; 14 auto r1 = [1, 2, 3].map!"a"; 15 auto r2 = [1, 2, 3].filter!"true"; 16 assert( equal([r1], [r2])); 17 18 assert( equal([1, 2, 3], [1, 2, 3])); 19 assert( equal([[1], [2], [3]], [[1], [2], [3]])); 20 21 static assert(!__traits(compiles, equal!(a => a)(1, "hi")));
1 struct S { 2 int x; 3 int y; 4 } 5 6 auto s1 = S(1, 2); 7 auto s2 = S(2, 2); 8 9 assert( equalBy!"y"(s1, s2)); 10 assert(!equalBy!"x"(s1, s2)); 11 12 auto r1 = [s2, s2]; 13 auto r2 = [s1, s1]; 14 15 assert( equalBy!"y"(r1, r2)); 16 assert(!equalBy!"x"(r1, r2));
Tells you if two things are equal