www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - struct opEquals bug

reply Sean Eskapp <eatingstaples gmail.com> writes:
Has this been reported?

struct A
{
	int x;

	A foo()
	{
		return A(x);
	}

	const bool opEquals(ref const A other)
	{
		return (x == other.x);
	}
}

void main()
{
	auto a = A(5);
	assert(a == a.foo); // Error
	assert(a.foo == a); // OK
}

The first assert fails to compile because a.foo isn't an lvalue, but the
second assert compiles fine. However, the language documentation states that

"...the expressions a.opEquals(b) and b.opEquals(a) are tried. If both resolve
to the same opEquals function, then the expression is rewritten to be
a.opEquals(b).
If one is a better match then the other, or one compiles and the other does
not, the one is selected.
Otherwise, an error results."

In this case, a.opEquals(b) doesn't compile, but b.opEquals(a) does, so it
should be selected.
Feb 17 2011
next sibling parent Jason House <jason.james.house gmail.com> writes:
Sean Eskapp Wrote:

 Has this been reported?
Do what any of us would do... Search bugzilla and that as a bug if you don't find it.
Feb 17 2011
prev sibling parent Don <nospam nospam.com> writes:
Sean Eskapp wrote:
 Has this been reported?
 
 struct A
 {
 	int x;
 
 	A foo()
 	{
 		return A(x);
 	}
 
 	const bool opEquals(ref const A other)
 	{
 		return (x == other.x);
 	}
 }
 
 void main()
 {
 	auto a = A(5);
 	assert(a == a.foo); // Error
 	assert(a.foo == a); // OK
 }
 
 The first assert fails to compile because a.foo isn't an lvalue, but the
 second assert compiles fine. However, the language documentation states that
 
 "...the expressions a.opEquals(b) and b.opEquals(a) are tried. If both resolve
 to the same opEquals function, then the expression is rewritten to be
 a.opEquals(b).
 If one is a better match then the other, or one compiles and the other does
 not, the one is selected.
 Otherwise, an error results."
 
 In this case, a.opEquals(b) doesn't compile, but b.opEquals(a) does, so it
 should be selected.
Yes, it is bug 3659.
Feb 17 2011