Random Suggestion: Swap Operator <=>?

Leandro Lucarella llucax at gmail.com
Fri Jul 17 07:46:12 PDT 2009


Julian Salazar, el 16 de julio a las 18:14 me escribiste:
> I'm wondering, who here would use a swap operator if it were available?
> 
> Something that would be normally achieved through:
> temp = a;
> a = b;
> b = temp;
> or for the more bit-wise ;)
> a ^= b;
> b ^= a;
> a ^= b;
> 
> It's something I've never actually seen implemented in any higher level 
> language, but finds uses in linked lists, binary trees, and other data 
> structures and algorithms. Temp wouldn't need to be an addressable value, so 
> theoretically this operator (which I propose to be "<=>") could be 
> compiler-optimized into an x86 xchg opcode for example. It could properly deal 
> with references, data structures, class instances, etc. and not require the 
> programmer to worry about these details.
> 
> I know that the chance of it actually being implemented is close to nil, but 
> even a template in the standard library for it would be cool (or did I miss 
> something?). Or am I being deluded? :) 

A more general solution is supporting tuples in the language, then you can
do something like:
a, b = b, a;

There you go (that's actually valid Python code, for example).

-- 
Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/
----------------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------------
En ese preciso instante ella con un leve gemido nos dice: "Ponla, Tito!
Ponla!".
	-- Sidharta Kiwi



More information about the Digitalmars-d mailing list