From b@rowling@on @ending from l@nc@@ter@@c@uk  Tue Oct 16 10:57:34 2018
From: b@rowling@on @ending from l@nc@@ter@@c@uk (Barry Rowlingson)
Date: Tue, 16 Oct 2018 09:57:34 +0100
Subject: [Rd] 
 sys.call() inside replacement functions incorrectly returns *tmp*
In-Reply-To: <e81bacdbba2846f39708e0c8c488f010@LNXP265MB1290.GBRP265.PROD.OUTLOOK.COM>
References: <CAB8pepwA52R9ZKOe7_83BMWzwsQcZ8eKG+wg3ihBL5+Qs5YPOQ@mail.gmail.com>
 <F139A45B-0BDF-41FC-A809-9A00AC2E79A0@dans.knaw.nl>
 <e81bacdbba2846f39708e0c8c488f010@LNXP265MB1290.GBRP265.PROD.OUTLOOK.COM>
Message-ID: <CANVKczOivkNjgqPKctABhqvn3x=t+_NvpjJGzb5Yx23JxM25iA@mail.gmail.com>

On Tue, Oct 16, 2018 at 12:03 AM Abs Spurdle <spurdle.a at gmail.com> wrote:

> Probably the best example I can think of is converting cartesian
> coordinates to polar coordinates.
> Then we might have something like (note, untested, written in my email):
> cart2polar = function (x, y)
>     list (theta=atan (y / x), r=sqrt (x * x + y * y) )
>
> massign (r, theta) = cart2polar (x, y)
>
> Now, I'm considering a multiple assignment operator, so something like:
> c (theta, r) $<-$ cart2polar (x, y)

This is something that comes up occasionally and as noted by Gabor,
has been implemented in packages.

But I am not keen on unpacking the return from a function into
multiple objects. The reason your `cart2polar` function returns a list
of theta and r is because it is returning a polar coordinate, and that
coordinate needs both. Why unpack them? If you don't need theta, then
do `r = cart2polar(x,y)$r`. If you need theta and r, then keep them
together in a single object. If you need to call a function  that
needs separate theta and r, use `plot(d$r, d$theta)`. Its a bit more
typing but that's a false efficiency when you want code to be tidy and
well-structured, and to convey meaning. `plot(this$r, this$theta)` is
clearly a plot of something to do with `this`, and you can see that
the r and the theta are coming from the same thing, whereas a
`(r,theta) %=% foo(x,y)` some place and then `plot(r, theta)`
somewhere else has broken the connection.

Barry



[Rd] sys.call() inside replacement functions incorrectly returns *tmp*
Barry Rowlingson