[R] with for objects

Thomas Lumley tlumley at u.washington.edu
Mon Dec 1 23:12:46 CET 2003


On Tue, 2 Dec 2003, Hadley Wickham wrote:

> Why not? A data frame is a convenient way of grouping related data
> together.  So is an object.  Writing with(expr, a + b) is just shorthand
> for writing expr$a + expr$b, so why shouldn't I be able to write
> with(obj, a + b) for obj at a + obj at b?

It is a bad idea because it has to break the information hiding that is an
important point of objects.

If you define a class "A" that either includes or inherits from another
class "B" then you don't know what slots your object has (without looking
at the internals of the implementation of class "B"). Suppose you define
slots a and b in addition to whatever you have inherited.
You don't know what
  with(obj, a+x)
will do: is it obj at a+x or obj at a+obj@x for some inherited slot x? And even
worse, if someone extends your class and adds a slot x in the subclass,
what should it do?

Now, as it happens, the function slotNames() will give you all the slots,
so it is possible to work out where x is, but this doesn't make it a good
idea.

Without rewriting the internal code for eval() it is also hard to do: it
would require either a recursive search through the expression changing
slot names to obj at slot, or a function to convert objects into environments
that could be fed to eval().


	-thomas




More information about the R-help mailing list