Skip to content
Prev 30106 / 63421 Next

Attributes of top level environments clobbered (was Re: [R] possible bug in function 'var' in R 2.7.2?)

On Fri, 3 Oct 2008, Gabor Grothendieck wrote:

            
Depends on your definitions.  Dispatch may work, but for me the show
stopper is unclass and related things.  Use of unclass inside code is
a fairly common idiom, and whenever an environment with a class gets
into a function that does an unclass on it, the environment ceases to
have a class.  You can argue that this should not be so but it is so,
it has been so for a long time, and it is lileky to remain so for a
long time.  To me this means that putting classes on environments is
too brittle to use in production code.  For this reason I would not use
a package that relies on doing this and would not encourage anyone
else to at this point.  I like prototype based OO, expecially for
graphics, which is why I wrote such a system for Lisp-Stat many years
ago. But using a variant of such a system in which the class can so
easily disappear seems ill-advised to me.
While these packages may be widely used their authors have disregarded
repeated advice on this.  Inheritance just does not work reliably for
environments. Encapsulation does work. (One could argue that
encapsulation would be a better choice even if reliable inheritance
was available, but that is a moot point).
There is a bug in lazy loading in that some important features of
environments are not preserved.  Given that attributes on environments
are not reliable however I would argue that the fact that they are not
preserved is not particularly important.  While fixing the more
important bugs it is probably not hard to fix the attribute issue as
well (that is a conditional statement -- fixing the more imortant bugs
is going to be fairly painful, which is why it hasn't happened yet.)
Once that is done, lazy loading may "work" for proto but using
attributes on environments is still a Bad Idea given the way R works.
There are some interesting poins on that page that are worth looking
into.  Over time I suspect all but the current 3. will be addressed,
but 3., which is a variant on the unclass issue, is not likely to be.
You can call this a deficiency in R if you like, and I would agree in
the sense that I think it is inappropriate to allow attributes to be
set but not in a reliable way because they can be inadvertenly
removed.  We should have done this differently.  THere were/are two
choices:

     Make reference values, including environments, special in that they
     may not have attributes. This woud have been fairly easy (modulo one
     use made in decorating the frames on the search path) and could be
     done now to clean things up.

     Make R-visible environments in two parts--a wrapper that is passed by
     value like standard R objects and could have attributes, and an
     internal part that is essentially the current environment object.
     This is analogous to the way that character vectors, even of length 1,
     consist of an STRSXP wrapper containing CHARSXPs that hold the string.
     The STRSXP's are visible at the R level, the CHARSXPs are not.  This
     would have been messier to implement, and unfortunately would be very
     messy to retro-fit at this point, so it isn't likely to happen unless
     there is some other compelling reason to do so.

The bottom line is that this situation isn't likely to change any time
soon as far as I can see.  If that means that for you R will not be
"fully functional" then so be it.  Attributes on environments are not
reliable and hence it is a Bad Idea to try to use them.  This is a
feature of R as it is now, has been for a while, and will be for a
while. If you write code for language X, you can write it for
X-as-it-s or X-as-you-wish-it-to-be; but if you chose
X-as-you-wish-it-to-be and find things don't work out it's hard to
argue that the fault is with X.

luke