From murdoch at stats.uwo.ca  Wed Feb  9 23:12:27 2005
From: murdoch at stats.uwo.ca (Duncan Murdoch)
Date: Wed Feb  9 23:11:04 2005
Subject: [Rd] Environment with no parent?
In-Reply-To: <x2r7jqmxzy.fsf@biostat.ku.dk>
References: <nl2i01h7pffsdrsmrfoft00ro069e5n8cu@4ax.com>
	<x24qgmojys.fsf@biostat.ku.dk>
	<esci01p52n37u6urqulun6ts464rju564c@4ax.com>
	<x2r7jqmxzy.fsf@biostat.ku.dk>
Message-ID: <ud2l015knhftvkmtf2ir78esrsqicbka12@4ax.com>

On 09 Feb 2005 00:29:37 +0100, Peter Dalgaard
<p.dalgaard@biostat.ku.dk> wrote :

>Duncan Murdoch <murdoch@stats.uwo.ca> writes:
>
>> >(a) efficiency. Is it expensive no longer to have the base functions
>> >bound directly to their symbol? (My gut feeling is that with suitable
>> >hashing and cacheing, the penalty is minimal.)
>> >
>> >(b) you can *only* use get and simple variable retrieval in a non-base
>> >environment with a NULL parent (eval(x <- 1, envir=foo) would give
>> >'couldn't find function "<-"' or so). This could cause some confusion.
>> 
>> (b) means that the default should stay the way it is, but I think
>> there should be a way to set up a truly empty environment.  We have a
>> fair number of cases where envir=NULL is used, so it would be safest
>> to make it a different value -- even if NULL is the obvious value for
>> an empty environment.
>
>Not necessarily. It just means that you should think about it. It is
>not a given that envir=NULL really means what the author expected, and
>fixing them up to read envir=.BaseEnv is probably quite doable.

For the benefit of the archives:

Setting the NULL environment to contain nothing is nontrivial; even
creating a new magic environment that appeared to be empty would
require a surprising number of low-level changes.  So, rather than
take this on, I've decided on this R-only solution to my problem:  a
version of exists() that treats NULL as if it were empty:

# Modified exists function:  like exists(x, envir, inherits = TRUE),
except that a 
# NULL parent is considered empty

myexists <- function(x, envir) {
    result <- FALSE
    while (!result && !is.null(envir)) {
    	result <- exists(x, envir=envir, inherits = FALSE)
    	envir <- parent.env(envir)
    }
    result
}

Duncan Murdoch


[Rd] Environment with no parent?
Duncan Murdoch