From murdoch at stats.uwo.ca  Thu Feb 25 15:37:14 2010
From: murdoch at stats.uwo.ca (Duncan Murdoch)
Date: Thu, 25 Feb 2010 09:37:14 -0500
Subject: [Rd] proto and baseenv()
In-Reply-To: <20100225075038.79ca9b63.misc7@emerose.org>
References: <20100225043854.GA26106@klutometis.wikitex.org>	<20100224233350.4e3da5db.misc7@emerose.org>	<4B8666E0.4070808@TU-Dresden.de>
	<20100225075038.79ca9b63.misc7@emerose.org>
Message-ID: <4B868B1A.5050409@stats.uwo.ca>

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
> NULL
>
> (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.
>
>
>



[Rd] proto and baseenv()
Duncan Murdoch