www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - opCmp function

reply "Bruno A. Costa" <bruno codata.com.br> writes:
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8Bit

Hi all,

I was playing with D operator overloading and I found the opCmp function a
bit strange. Why the type returned is an int? It would not have to return a
bit?

And how should I code an opCmp overload? I tried the some coding (atached)
and it worked well, but I am not sure if the opCmp overloading (at the end)
is Ok.

Thanks,

Bruno.
May 03 2004
next sibling parent J Anderson <REMOVEanderson badmama.com.au> writes:
Bruno A. Costa wrote:

Hi all,

I was playing with D operator overloading and I found the opCmp function a
bit strange. Why the type returned is an int? It would not have to return a
bit?
  

opCmp returns 3 states 0: equal -1: less then 1: more then
And how should I code an opCmp overload? I tried the some coding (atached)
and it worked well, but I am not sure if the opCmp overloading (at the end)
is Ok.

	int opCmp (Byte bt) {
		return (_value - bt.value);
	}

-- -Anderson: http://badmama.com.au/~anderson/
May 03 2004
prev sibling parent reply Ant <Ant_member pathlink.com> writes:
In article <c75vi8$2q14$1 digitaldaemon.com>, Bruno A. Costa says...
--nextPart1697271.eHQoPkZ5kg
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8Bit

Hi all,

I was playing with D operator overloading and I found the opCmp function a
bit strange. Why the type returned is an int? It would not have to return a
bit?

0 means equal, <0 means one is lower, >0 means the other is lower. (it's common practice)
And how should I code an opCmp overload? I tried the some coding (atached)
and it worked well, but I am not sure if the opCmp overloading (at the end)
is Ok.

I fell on that trap once so I should help now. you must override int opComp(Object) not create a new int opComp(Byte) So, are you doing an all OO lib? or just what you need? Ant
May 03 2004
next sibling parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
Ant wrote:

In article <c75vi8$2q14$1 digitaldaemon.com>, Bruno A. Costa says...
  

--nextPart1697271.eHQoPkZ5kg
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8Bit

Hi all,

I was playing with D operator overloading and I found the opCmp function a
bit strange. Why the type returned is an int? It would not have to return a
bit?
    

0 means equal, <0 means one is lower, >0 means the other is lower. (it's common practice)
And how should I code an opCmp overload? I tried the some coding (atached)
and it worked well, but I am not sure if the opCmp overloading (at the end)
is Ok.
    

I fell on that trap once so I should help now. you must override int opComp(Object) not create a new int opComp(Byte)

{ byte val; int opCmp(A _a) { return val - _a.val; } } int main ( char [] [] args ) { A a = new A; A b = new A; a.val = 10; b.val = 11; printf("%d < %d = %d\n", a.val, b.val, b < a); } This works. What are you talking about? You do need to do such things if you want to use sort with arrays (which I think is a bug). Other then that it should work fine.
So, are you doing an all OO lib? or just what you need?

Ant



  

-- -Anderson: http://badmama.com.au/~anderson/
May 03 2004
parent Ant <Ant_member pathlink.com> writes:
In article <c766ek$32u$1 digitaldaemon.com>, J Anderson says...
Ant wrote:

In article <c75vi8$2q14$1 digitaldaemon.com>, Bruno A. Costa says...
  

--nextPart1697271.eHQoPkZ5kg
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8Bit

Hi all,

I was playing with D operator overloading and I found the opCmp function a
bit strange. Why the type returned is an int? It would not have to return a
bit?
    

0 means equal, <0 means one is lower, >0 means the other is lower. (it's common practice)
And how should I code an opCmp overload? I tried the some coding (atached)
and it worked well, but I am not sure if the opCmp overloading (at the end)
is Ok.
    

I fell on that trap once so I should help now. you must override int opComp(Object) not create a new int opComp(Byte)

{ byte val; int opCmp(A _a) { return val - _a.val; } } int main ( char [] [] args ) { A a = new A; A b = new A; a.val = 10; b.val = 11; printf("%d < %d = %d\n", a.val, b.val, b < a); } This works. What are you talking about?

http://www.digitalmars.com/d/phobos.html#object int cmp(Object obj) Compare with another Object obj. Returns: <0 for (this < obj) =0 for (this == obj)
0 for (this > obj) 

Ant
May 03 2004
prev sibling parent "Bruno A. Costa" <bruno codata.com.br> writes:
Ant wrote:

 In article <c75vi8$2q14$1 digitaldaemon.com>, Bruno A. Costa says...
--nextPart1697271.eHQoPkZ5kg
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8Bit

Hi all,

I was playing with D operator overloading and I found the opCmp function a
bit strange. Why the type returned is an int? It would not have to return
a bit?

0 means equal, <0 means one is lower, >0 means the other is lower. (it's common practice)
And how should I code an opCmp overload? I tried the some coding (atached)
and it worked well, but I am not sure if the opCmp overloading (at the
end) is Ok.

I fell on that trap once so I should help now. you must override int opComp(Object) not create a new int opComp(Byte) So, are you doing an all OO lib? or just what you need? Ant

I just wanted to learn D, so I started to write some easy wrappers to primitive types. But an OO lib would be a very nice thing to do. Bruno.
May 03 2004