Issue 3789, stucts equality
Artur Skawina
art.08.09 at gmail.com
Thu Mar 29 11:14:10 PDT 2012
On 03/29/12 14:07, Andrej Mitrovic wrote:
> 3/29/12, Andrej Mitrovic <andrej.mitrovich at gmail.com> wrote:
>> snip
>
> Crap, lockstep doesn't work on different types, it was iterating over
> the arrays. That went over my head. A slightly complicated version
> that actually works:
>
> import std.stdio;
> import std.conv;
>
> string tupleCompare(int len)()
> {
> string result;
> foreach (i; 0 .. len)
> {
> result ~= "
> if (this.tupleof[" ~ to!string(i) ~ "] != t.tupleof[" ~
> to!string(i) ~ "])
> return false;
> ";
> }
> return result;
> }
>
> template safeOpEquals()
> {
> bool opEquals(typeof(this) t)
> {
> enum len = typeof(this).tupleof.length;
> mixin(tupleCompare!len);
> return true;
> }
> }
// D does make things easy:
template safeOpEquals()
{
bool opEquals(T)(T t) {
foreach (i, v; this.tupleof )
if (v != t.tupleof[i])
return 0;
return 1;
}
}
struct Foo
{
int[] arr;
string s;
mixin safeOpEquals;
}
You may also want to include something like
> static if (!is(typeof(this)==T))
> return 0;
> else
> foreach etc...
to make it return false, instead of failing at compiletime when
comparing different types.
artur
More information about the Digitalmars-d
mailing list