[Rd] classed

Bill Venables Bill.Venables@cmis.csiro.au
Fri, 15 Sep 2000 14:21:29 +1000


At 15:00 15/09/00 +1100, Telford Tendys wrote:
>On Thu, Sep 14, 2000 at 10:31:26AM -0400, Paul Gilbert wrote:
>> classed <- function (x, cls)
>> {
>>     class(x) <- cls
>>     x
>> }
>[snip]
>> The function is useful not so much because it saves a line of code
>> in other functions, but because I think it encourages a cleaner, easier
to read
>> style.
>
>Just to add my 2c, I don't believe it is cleaner to hide assignment
>statements inside functions without some reasonably sensible reason to
>do so. When I see something like:
>
>	y <- wibble( x, "foo" );
>
>I would expect `x' not to change. 

And nor does it.  What happens is that a new object is constructed from x,
with (in this case) extra bits and pieces tacked on and (here) that new
object is assigned the name y.

Writing a function to modify its argument is possible (e.g. fix() does it)
but not altogether straightforward, especially if you want it to work in
the most general case.

> Now OK this isn't a watertight rule
>(nothing ever is) but little functions containing hidden assignment
>statements are usually confusing to a reader not already familiar
>with the code.

If that was what was happening I would agree with you, but it isn't.  You
have been misled by the old function/macro thing.  What was presented was a
true function but you read it as if it were a macro.  

Bill V.


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._