Issue 3789, stucts equality
Andrej Mitrovic
andrej.mitrovich at gmail.com
Thu Mar 29 04:54:02 PDT 2012
On 3/29/12, Andrej Mitrovic <andrej.mitrovich at gmail.com> wrote:
> Speaking of which, does anyone have some sort of template to
> autogenerate an opEquals that compares all fields of a struct via
> '=='?
Well that took 2 seconds.
import std.range;
template safeOpEquals(T)
{
bool opEquals(T t)
{
foreach (lhsField, rhsField; lockstep(this.tupleof, t.tupleof))
{
if (lhsField != rhsField)
return false;
}
return true;
}
}
struct Foo
{
int[] arr;
mixin safeOpEquals!Foo;
}
void main()
{
Foo a, b;
a.arr = [1, 2, 3].dup;
b.arr = [1, 2, 3].dup;
assert(a == b);
}
D makes things so damn easy.
More information about the Digitalmars-d
mailing list