Ideas from the Chapel language (swap)

Bill Baxter dnewsgroup at billbaxter.com
Thu Oct 4 10:31:41 PDT 2007


Derek Parnell wrote:
> On Thu, 04 Oct 2007 17:55:05 +0900, Bill Baxter wrote:
> 
>> I find it hard to believe that a compiler couldn't recognize that this 
>> is a swap operation:
>>     tmp = a;
>>     a = b;
>>     b = tmp;
>>
>> If it's not harder than I think for some reason, then it's not really 
>> needed in the language.
> 
> Maybe it could, but that's not the point of a programming language. A
> programming language is for people, not computers, to read. It is whole lot
> easy to recognise "swap(a,b)" or even "a<->b" as a swapping operation than
> three lines of code.

Right, so you put those three lines in a template function called 
"swap".  If the compiler can recognize that's exchanging the values of a 
and b, then there's no need for swap to be a compiler intrinsic.  The 
plain old function is good enough.

--bb



More information about the Digitalmars-d mailing list