From pcd at roxygen.org  Thu Feb 25 10:58:15 2010
From: pcd at roxygen.org (Peter Danenberg)
Date: Thu, 25 Feb 2010 03:58:15 -0600
Subject: [Rd] proto and baseenv()
In-Reply-To: <20100224233153.a254588f.ben@emerose.org>
References: <20100225043854.GA26106@klutometis.wikitex.org>
	<20100224233153.a254588f.ben@emerose.org>
Message-ID: <20100225095815.GA31673@klutometis.wikitex.org>

Quoth Ben Escoto on Sweetmorn, the 56th of Chaos:
> I'm new to proto, but it seems like this is also a big drawback:
> 
> > z <- 1
> > proto(baseenv(), expr={a=z})$a
> Error in eval(expr, envir, enclos) : object "z" not found

Ouch, good point; it may be that parent pollution is the lesser of two
evils, then.

It's a little bit of a catch 22, it seems; because even if you ascend
up the parent hierarchy until you hit a non-prototype, the first
environment that has `a' also has `z':

  > library(proto)
  > get.proto <- function(x, env) {
  +   print(list(env=env,
  +              ls=ls(env, all.names=TRUE)))
  +   if (is.proto(env))
  +     tryCatch(get(x, env=env, inherits=FALSE),
  +              error=function(e) get.proto(x, env$.super))
  +   else
  +     stop(sprintf('object \'%s\' not found', x))
  +
  + }
  >
  > (function() { z <- 1; get.proto('a', proto(eval={a=z})) })()
  $env
  <environment: 0x1d12180>
  attr(,"class")
  [1] "proto"       "environment"

  $ls
  [1] ".super" ".that"  "eval"

  $env
  <environment: 0x1d12918>

  $ls
  [1] "a" "z"

  Error in get.proto(x, env$.super) : object 'a' not found
  > 

So, even if the $ environment diverged, say, from the eval
environment; the first relevant environment is already polluted.

It almost seems like you have to choose between eval and parent
pollution (unless I'm missing something).



[Rd] proto and baseenv()
Peter Danenberg