digitalmars.D - Re: Ideas from the Chapel language (swap)
- Bruce Adams (4/27) Oct 04 2007 I think having at least a library function is necessary. I'm not 100% su...
Bill Baxter Wrote:Derek Parnell wrote:I think having at least a library function is necessary. I'm not 100% sure it is sufficient. The best way to implement it depends on the nature of the data structure that is being swapped. I don't know if a library function can be generic enough to get this right every time with templates it could come quite close but the size of structure where swap members becomes more efficient than swap via a temporary is hard to code in a template. The answer in the first case is to require the user to create an explict specialisation when this becomes an issue. When you start to bring threads into the equation things can get even more fun. Bruce.On Thu, 04 Oct 2007 17:55:05 +0900, Bill Baxter wrote: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. --bbI 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.
Oct 04 2007