www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Exchange Operator

reply "Tu Nam" <dreamweaver mail15.com> writes:
I see in Extended C :http://web.ss.pub.ro/~vycxs/ecdk/ecl_x.htm
has an exchange operator which very , very notable :
x=0;
y=3;
Just write :
x<->y so x=3;y=0;
Whether D can make a builtin exchange operator like that , and a optExchange
for exchange operator overloading ?
May 09 2004
next sibling parent reply "Walter" <newshound digitalmars.com> writes:
"Tu Nam" <dreamweaver mail15.com> wrote in message
news:c7mtv3$10p4$1 digitaldaemon.com...
 I see in Extended C :http://web.ss.pub.ro/~vycxs/ecdk/ecl_x.htm
 has an exchange operator which very , very notable :
 x=0;
 y=3;
 Just write :
 x<->y so x=3;y=0;
 Whether D can make a builtin exchange operator like that , and a

 for exchange operator overloading ?

This comes up now and then, and it's a seductive idea. But there just doesn't seem to be enough utility in it to justify making it an operator.
May 09 2004
parent reply "Tu Nam" <dreamweaver mail15.com> writes:
Well , the first use I just thought is in sorting algorithms.
That operator is more be useful in sort .
"Walter" <newshound digitalmars.com> wrote in message
news:c7muj8$11je$1 digitaldaemon.com...
 "Tu Nam" <dreamweaver mail15.com> wrote in message
 news:c7mtv3$10p4$1 digitaldaemon.com...
 I see in Extended C :http://web.ss.pub.ro/~vycxs/ecdk/ecl_x.htm
 has an exchange operator which very , very notable :
 x=0;
 y=3;
 Just write :
 x<->y so x=3;y=0;
 Whether D can make a builtin exchange operator like that , and a

 for exchange operator overloading ?

This comes up now and then, and it's a seductive idea. But there just doesn't seem to be enough utility in it to justify making it an operator.

May 10 2004
parent reply Ilya Minkov <minkov cs.tum.edu> writes:
How about a function tenplate in a coming up standard template library?

-eye

Tu Nam schrieb:

 Well , the first use I just thought is in sorting algorithms.
 That operator is more be useful in sort .

May 10 2004
parent "Matthew" <matthew.hat stlsoft.dot.org> writes:
Yes, I've added one in DTL. But because templates are not implicitly
instantiated, it's a fair amount of eye candy.

"Ilya Minkov" <minkov cs.tum.edu> wrote in message
news:c7oho2$fnn$2 digitaldaemon.com...
 How about a function tenplate in a coming up standard template library?

 -eye

 Tu Nam schrieb:

 Well , the first use I just thought is in sorting algorithms.
 That operator is more be useful in sort .


May 10 2004
prev sibling next sibling parent "Matthew" <matthew.hat stlsoft.dot.org> writes:
I'd very much like this, but only if opSwap was available for UDTs

"Tu Nam" <dreamweaver mail15.com> wrote in message
news:c7mtv3$10p4$1 digitaldaemon.com...
 I see in Extended C :http://web.ss.pub.ro/~vycxs/ecdk/ecl_x.htm
 has an exchange operator which very , very notable :
 x=0;
 y=3;
 Just write :
 x<->y so x=3;y=0;
 Whether D can make a builtin exchange operator like that , and a optExchange
 for exchange operator overloading ?

May 09 2004
prev sibling parent reply Alex Fitzpatrick <alex.nospam.fitzpatrick videotron.nospam.ca> writes:
Tu Nam wrote:
 I see in Extended C :http://web.ss.pub.ro/~vycxs/ecdk/ecl_x.htm
 has an exchange operator which very , very notable :
 x=0;
 y=3;
 Just write :
 x<->y so x=3;y=0;
 Whether D can make a builtin exchange operator like that , and a optExchange
 for exchange operator overloading ?
 
 

I like the idea... it should be possible to optimise for lots of types too. Just to keep the conversation going, how about: x<=>y or x:=:y or x:-:y or x<~>y -- Alex
May 10 2004
parent reply "Matthew" <matthew.hat stlsoft.dot.org> writes:
 Tu Nam wrote:
 I see in Extended C :http://web.ss.pub.ro/~vycxs/ecdk/ecl_x.htm
 has an exchange operator which very , very notable :
 x=0;
 y=3;
 Just write :
 x<->y so x=3;y=0;
 Whether D can make a builtin exchange operator like that , and a optExchange
 for exchange operator overloading ?

I like the idea... it should be possible to optimise for lots of types too. Just to keep the conversation going, how about: x<=>y

That's already an operator (comparison) in Ruby. To overload that would be confusing.
 or
 x:=:y

 or

 x:-:y

 or

 x<~>y

None of them are as clear, to me at least, as <->
May 10 2004
parent reply Juan C <Juan_member pathlink.com> writes:
<snip>
 I see in Extended C :http://web.ss.pub.ro/~vycxs/ecdk/ecl_x.htm
 has an exchange operator which very , very notable :
 x=0;
 y=3;
 Just write :
 x<->y so x=3;y=0;
 Whether D can make a builtin exchange operator like that , and a optExchange
 for exchange operator overloading ?



I dunno... of course I'm against any new operators anyway, but this in particular may cause trouble, methinks. I expect it would be fine for ints and such, but for instances of objects I would expect it to simply exchange the values of the references, not the contents (certainly much faster). But this is where the trouble would come in, what if I have an array of MyObjs that I want to sort (with a bubble sort for instance) and use two references: Ofront and Oback, when I use: Ofront <-> OBack to exchange them it would have no effect on the array. So newbies would have to be trained to use indices rather than references to array members: MyArray [ ifront ] <-> MyArray [ iback ] which is not as clear, but better than not having the exchange operator. Certainly it has some value, but also causes potential for trouble. Anyway... mightn't it be better to leave well enough alone and not cause the same kind of confusion that that D strings cause with printf() ?
May 10 2004
parent Norbert Nemec <Norbert.Nemec gmx.de> writes:
Juan C wrote:

 I dunno... of course I'm against any new operators anyway,

I agree in so far, that new operators should be consider with special care. They should be descriptive, and they should not be used where similar actions might be mixed up. They should not be overloaded with different meaning in different context. Apart from that, I really have no problems with new operators. The fact that I have to learn something new does not mean that it is bad.
 I expect it would be fine for ints and such, but for instances of objects
 I would expect it to simply exchange the values of the references, not the
 contents (certainly much faster). But this is where the trouble would come
 in, what if I have an array of MyObjs that I want to sort (with a bubble
 sort for instance) and use two references: Ofront and Oback, when I use:
 
 Ofront <-> OBack
 
 to exchange them it would have no effect on the array. So newbies would
 have to be trained to use indices rather than references to array members:
 
 MyArray [ ifront ] <-> MyArray [ iback ]
 
 which is not as clear, but better than not having the exchange operator.
 Certainly it has some value, but also causes potential for trouble.

I don't think that confusion would be worse than for the = operator.
May 10 2004