Idea: swap with multiple arguments

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Wed May 25 11:36:04 PDT 2016


On 05/25/2016 01:08 PM, Martin Tschierschke wrote:
> On Monday, 23 May 2016 at 20:01:08 UTC, Andrei Alexandrescu wrote:
>> So swap(a, b) swaps the contents of a and b. This could be easily
>> generalized to multiple arguments such that swap(a1, a2, ..., an)
>> arranges things such that a1 gets an, a2 gets a1, a3 gets a2, etc. I
>> do know applications for three arguments. Thoughts? -- Andrei
>
> A newbee question about language design:
> When I looked first time at Ruby I liked the simple a,b = b,a syntax,
> so swap. Would it be theoretically possible to allow this?
>
> And if not, where does it breaks the general language design?
>
> Best regards mt.

No fundamental breakage except probably for DRY violation (consider a 
and b may be arbitrarily complicated expressions yielding lvalues). 
Also, if expressions are involved everything must be carefully defined 
so e.g.

(a[f()], b[i++]) = (b[i++], a[f()])

defines what operations are executed and in what order.

The swap assignment is cute and I used to like it a lot more until I 
figured adds more problems than it solves.


Andrei



More information about the Digitalmars-d mailing list