www.digitalmars.com         C & C++   DMDScript  

D - Operator Overloading

reply "Bruno A. Costa" <bruno codata.com.br> writes:
Hi all,

Someone has links to examples of operator overloading in D? I read the
specification, but I was not able to understand it correctly.

I noticed that D doesn't have the operator keyword. I think that this 
keyword gives more flexibility and power than the simple use of functions
(opCmp, opEquals etc). IMHO, it should be added in D.

Thanks,

Bruno.
Apr 27 2004
next sibling parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Bruno A. Costa wrote:

 Hi all,
 
 Someone has links to examples of operator overloading in D? I read the
 specification, but I was not able to understand it correctly.
It seemed straightforward to me LTIL. Anywhere particular that you're having trouble?
 I noticed that D doesn't have the operator keyword. I think that this 
 keyword gives more flexibility and power than the simple use of functions
 (opCmp, opEquals etc). IMHO, it should be added in D.
So you can bend the semantics as much as you like? Stewart. -- My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit.
Apr 27 2004
parent reply "Bruno A. Costa" <bruno codata.com.br> writes:
Stewart Gordon wrote:

 
 Someone has links to examples of operator overloading in D? I read the
 specification, but I was not able to understand it correctly.
It seemed straightforward to me LTIL. Anywhere particular that you're having trouble?
I just couldn't understand how to write the code. Maybe I am too tied to the c++ syntax. But I am looking to the example from J C Calvarese (http:/ www.dsource.org/tutorials/index.php?show_example=43) and things are becoming more clear.
 I noticed that D doesn't have the operator keyword. I think that this
 keyword gives more flexibility and power than the simple use of functions
 (opCmp, opEquals etc). IMHO, it should be added in D.
So you can bend the semantics as much as you like?
With the operator keyword I can write ONLY the overloadings I want. With opCmp() I overload all comparison operators. Obviously this approuch has pros and cons. I would like to have both in a language - opFunctions and operator keyword. Thanks, Bruno.
Apr 27 2004
next sibling parent Stewart Gordon <smjg_1998 yahoo.com> writes:
Bruno A. Costa wrote:
<snip>
 So you can bend the semantics as much as you like?
With the operator keyword I can write ONLY the overloadings I want. With opCmp() I overload all comparison operators. Obviously this approuch has pros and cons.
<snip> If you're going to implement a less than comparison, to me it seems pointless to go out of your way not to have greater than comparison to match. And similarly <= and >=.... Stewart. -- My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit.
Apr 28 2004
prev sibling parent "Unknown W. Brackets" <unknown at.simplemachines.dot.org> writes:
Bruno A. Costa wrote:

 With the operator keyword I can write ONLY the overloadings I want. With
 opCmp() I overload all comparison operators. Obviously this approuch has
 pros and cons. I would like to have both in a language - opFunctions and
 operator keyword.
I think the difference is this: in C++, operators could be, and indeed were, logically misused. By this, I mean that the + operator might be used for removing something, for all you might know. Now, it sounds to me if you only want to write one operator, this is what you're looking to do. If you were planning to keep the operators logical, it would only follow that you need all the operators. Part of D is making usable, self documenting code. If + means anything but add, if < means move or something... it doesn't achieve the above purpose. For those classes using operators for the purposes they were meant for, having only to do a percentage of overloads makes it easier and better, not worse. -[Unknown]
Apr 28 2004
prev sibling parent reply FlyTox <rox271 hotmail.com> writes:
I have to confess I do not understand what was wrong with the C++ 
syntax. I find it oakward to type "opAddAssign" rather than "+=". 
Eventually the compiler calls opAddAsign when it reaches +=; what is the 
benefit of such a name discrepancy?

Sorry I may not get the point but as far as I understand, this D 
operator replacement achieves exactly the same as C++. Why changing 
things when there is no additional benefit? Just to give D an identity?

Bruno A. Costa wrote:
 Hi all,
 
 Someone has links to examples of operator overloading in D? I read the
 specification, but I was not able to understand it correctly.
 
 I noticed that D doesn't have the operator keyword. I think that this 
 keyword gives more flexibility and power than the simple use of functions
 (opCmp, opEquals etc). IMHO, it should be added in D.
 
 Thanks,
 
 Bruno.
Apr 27 2004
next sibling parent Ant <Ant_member pathlink.com> writes:
In article <c6m4i3$2829$1 digitaldaemon.com>, FlyTox says...
I have to confess I do not understand what was wrong with the C++ 
syntax. I find it oakward to type "opAddAssign" rather than "+=". 
Eventually the compiler calls opAddAsign when it reaches +=; what is the 
benefit of such a name discrepancy?

Sorry I may not get the point but as far as I understand, this D 
operator replacement achieves exactly the same as C++. Why changing 
things when there is no additional benefit? Just to give D an identity?
This was before my time here but I believe that it was discussed on this new group and the consensus was the new version. Maybe you can find the discussions on the group. Why not move this discussion to the new group? Ant
Apr 27 2004
prev sibling parent reply "Walter" <newshound digitalmars.com> writes:
"FlyTox" <rox271 hotmail.com> wrote in message
news:c6m4i3$2829$1 digitaldaemon.com...
 I have to confess I do not understand what was wrong with the C++
 syntax. I find it oakward to type "opAddAssign" rather than "+=".
 Eventually the compiler calls opAddAsign when it reaches +=; what is the
 benefit of such a name discrepancy?
Consider the ++ operator, and the kludges C++ uses to distinguish pre from post increment.
 Sorry I may not get the point but as far as I understand, this D
 operator replacement achieves exactly the same as C++. Why changing
 things when there is no additional benefit? Just to give D an identity?
There's quite a bit of benefit. For example, a single D function opCmp replaces 8 C++ functions a<b, a<=b, a>b, a>=b, b<a, b<=a, b>a, b>=a. I'd rather write and debug just one.
May 07 2004
parent "Matthew" <matthew.hat stlsoft.dot.org> writes:
"Walter" <newshound digitalmars.com> wrote in message
news:c7fhga$2gs2$1 digitaldaemon.com...
 "FlyTox" <rox271 hotmail.com> wrote in message
 news:c6m4i3$2829$1 digitaldaemon.com...
 I have to confess I do not understand what was wrong with the C++
 syntax. I find it oakward to type "opAddAssign" rather than "+=".
 Eventually the compiler calls opAddAsign when it reaches +=; what is the
 benefit of such a name discrepancy?
Consider the ++ operator, and the kludges C++ uses to distinguish pre from post increment.
Never mind the evil inconsistencies between compilers which either, correctly, fail to compile an expression with one or the other that is not provided by a UDT, and those that simply insert the other. Nasty stuff.
 Sorry I may not get the point but as far as I understand, this D
 operator replacement achieves exactly the same as C++. Why changing
 things when there is no additional benefit? Just to give D an identity?
There's quite a bit of benefit. For example, a single D function opCmp replaces 8 C++ functions a<b, a<=b, a>b, a>=b, b<a, b<=a, b>a, b>=a. I'd rather write and debug just one.
May 07 2004