www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - opCmp

reply Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
Good day.

There's a problem in how opCmp works.

I have a structure, that represents an element of a range. Let's say a
character. That character can be invalid.
I need all comparison operators to return false of at least one of the
operands is invalid.
with opCmp, the expression a   b is rewritten as a.opCmp(B)   0, which
doesn't allow me to define such a logic.
wouldn't it be better to change the rewrite of opCmp to test for exact
values -1, 0 and 1? In that case I could return 2 and have all
comparisons fail.

-- 
Bye,
Gor Gyolchanyan.
Feb 03 2012
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 02/03/2012 06:44 AM, Gor Gyolchanyan wrote:
 Good day.

 There's a problem in how opCmp works.

 I have a structure, that represents an element of a range. Let's say a
 character. That character can be invalid.
 I need all comparison operators to return false of at least one of the
 operands is invalid.
As an observation, you want to implement the concept of "unordered" for types, similar to floating point types: http://dlang.org/expression.html#floating_point_comparisons I am very surprised that the following operator works with non-floating-point types: class C { override int opCmp(Object o) { return 0; } } void main() { auto c = new C; auto b = (c !<>= c); // <-- compiles! int i, j; auto b2 = (i !<>= j); // <-- compiles! } Is that supported? Is it a bug? Would using those /unordered/ operator help in your case?
 with opCmp, the expression a   b is rewritten as a.opCmp(B)   0, which
 doesn't allow me to define such a logic.
 wouldn't it be better to change the rewrite of opCmp to test for exact
 values -1, 0 and 1? In that case I could return 2 and have all
 comparisons fail.
Ali
Feb 03 2012
next sibling parent reply "Daniel Murphy" <yebblies nospamgmail.com> writes:
Bug!

"Ali Çehreli" <acehreli yahoo.com> wrote in message 
news:jgh2nb$rtv$1 digitalmars.com...
 On 02/03/2012 06:44 AM, Gor Gyolchanyan wrote:
 Good day.

 There's a problem in how opCmp works.

 I have a structure, that represents an element of a range. Let's say a
 character. That character can be invalid.
 I need all comparison operators to return false of at least one of the
 operands is invalid.
As an observation, you want to implement the concept of "unordered" for types, similar to floating point types: http://dlang.org/expression.html#floating_point_comparisons I am very surprised that the following operator works with non-floating-point types: class C { override int opCmp(Object o) { return 0; } } void main() { auto c = new C; auto b = (c !<>= c); // <-- compiles! int i, j; auto b2 = (i !<>= j); // <-- compiles! } Is that supported? Is it a bug? Would using those /unordered/ operator help in your case?
 with opCmp, the expression a   b is rewritten as a.opCmp(B)   0, which
 doesn't allow me to define such a logic.
 wouldn't it be better to change the rewrite of opCmp to test for exact
 values -1, 0 and 1? In that case I could return 2 and have all
 comparisons fail.
Ali
Feb 03 2012
parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3D7430
Please vote on this, so it would get some attention.

On Fri, Feb 3, 2012 at 8:45 PM, Daniel Murphy <yebblies nospamgmail.com> wr=
ote:
 Bug!

 "Ali =C3=87ehreli" <acehreli yahoo.com> wrote in message
 news:jgh2nb$rtv$1 digitalmars.com...
 On 02/03/2012 06:44 AM, Gor Gyolchanyan wrote:
 Good day.

 There's a problem in how opCmp works.

 I have a structure, that represents an element of a range. Let's say a
 character. That character can be invalid.
 I need all comparison operators to return false of at least one of the
 operands is invalid.
As an observation, you want to implement the concept of "unordered" for types, similar to floating point types: =C2=A0 http://dlang.org/expression.html#floating_point_comparisons I am very surprised that the following operator works with non-floating-point types: class C { =C2=A0 =C2=A0 override int opCmp(Object o) =C2=A0 =C2=A0 { =C2=A0 =C2=A0 =C2=A0 =C2=A0 return 0; =C2=A0 =C2=A0 } } void main() { =C2=A0 =C2=A0 auto c =3D new C; =C2=A0 =C2=A0 auto b =3D (c !<>=3D c); =C2=A0 =C2=A0 =C2=A0// <-- compil=
es!
 =C2=A0 =C2=A0 int i, j;
 =C2=A0 =C2=A0 auto b2 =3D (i !<>=3D j); =C2=A0 =C2=A0 // <-- compiles!
 }

 Is that supported? Is it a bug? Would using those /unordered/ operator
 help in your case?

 with opCmp, the expression a   b is rewritten as a.opCmp(B)   0, which
 doesn't allow me to define such a logic.
 wouldn't it be better to change the rewrite of opCmp to test for exact
 values -1, 0 and 1? In that case I could return 2 and have all
 comparisons fail.
Ali
--=20 Bye, Gor Gyolchanyan.
Feb 03 2012
prev sibling parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
How do I overload the unordered comparison operators? do I overload
them one by one? If I do what happens if I also define the opCmp?

On Fri, Feb 3, 2012 at 8:41 PM, Ali =C3=87ehreli <acehreli yahoo.com> wrote=
:
 On 02/03/2012 06:44 AM, Gor Gyolchanyan wrote:
 Good day.

 There's a problem in how opCmp works.

 I have a structure, that represents an element of a range. Let's say a
 character. That character can be invalid.
 I need all comparison operators to return false of at least one of the
 operands is invalid.
As an observation, you want to implement the concept of "unordered" for types, similar to floating point types: =C2=A0http://dlang.org/expression.html#floating_point_comparisons I am very surprised that the following operator works with non-floating-point types: class C { =C2=A0 =C2=A0override int opCmp(Object o) =C2=A0 =C2=A0{ =C2=A0 =C2=A0 =C2=A0 =C2=A0return 0; =C2=A0 =C2=A0} } void main() { =C2=A0 =C2=A0auto c =3D new C; =C2=A0 =C2=A0auto b =3D (c !<>=3D c); =C2=A0 =C2=A0 =C2=A0// <-- compiles=
!
 =C2=A0 =C2=A0int i, j;
 =C2=A0 =C2=A0auto b2 =3D (i !<>=3D j); =C2=A0 =C2=A0 // <-- compiles!
 }

 Is that supported? Is it a bug? Would using those /unordered/ operator he=
lp
 in your case?


 with opCmp, the expression a   b is rewritten as a.opCmp(B)   0, which
 doesn't allow me to define such a logic.
 wouldn't it be better to change the rewrite of opCmp to test for exact
 values -1, 0 and 1? In that case I could return 2 and have all
 comparisons fail.
Ali
--=20 Bye, Gor Gyolchanyan.
Feb 03 2012