[Rd] proto and baseenv()

Duncan Murdoch murdoch at stats.uwo.ca
Thu Feb 25 15:37:14 CET 2010

On 25/02/2010 8:50 AM, Ben wrote:
> I was disappointed in this behavior because it seems error-prone.
> Suppose I declare an environment
> > b <- 1
> > p <- proto(expr={
>     a <- 2
> +   ...
> + })
> > p$a
> [1] 2
> > p$b
> [1] 1
> Presumably if I ask for p$a or p$b later, it's because I'm interesting
> in the value of "p$a" or "p$b" that I specifically put inside that
> environment.  Otherwise I would just ask for "a" or "b".  If I'm
> asking for "p$b" it the above case, that means I forgot to declare b
> inside p.  In this case there should be an error telling me that, not
> a silent substitution of the wrong quantity.

I think you are looking for a different object model than proto offers.  
There aren't many languages that offer the prototype object model.
> If someone wanted to do the y$ls() thing, they could always
> > y <- proto(a=1)
> > with(y, ls())
> [1] "a"
> Another reason is that there are plenty of other programming languages
> that have similar structures and this behavior is very odd. 

Which languages are you thinking of?  The paper about proto only 
mentions Self, LispStat, and Javascript. The Wikipedia page 
http://en.wikipedia.org/wiki/Prototype-based_programming has a long list 
of obscure ones, but the only additional ones I've used are R, Perl and 
Tcl, all with add-on packages.

Duncan Murdoch

>  In
> standard languages asking for "b" inside the "p" object gives you an
> error, and no one complains.  Even in R, we have this behavior:
> > z <- 1
> > list(a=3)$z
> (Actually I think the above should be an error, but at least it isn't
> 1.)  So anyway, I'm not saying that p$b being 1 is an outright 2+2=5
> bug, but it does seem to be surprising behavior that leads to bugs.
> But I'm sure you're right that there are historical/structural reasons
> for this to be the case, so perhaps there's no solution.

More information about the R-devel mailing list