Prof Brian Ripley <[EMAIL PROTECTED]> writes:
> On Thu, 2 Feb 2006, Fernando Saldanha wrote:
>
> > I am trying to imitate "encapsulation" from other languages like Java
> > or C++. Coming from that background, it bothers me that I can commit
> > errors like the following:
> >
> >> x <- 1
> >> f <- function(z) { y <- x; y + 1 } # Mistake: I should have written y <- z
> >> f(10)
> > [1] 2
> >
> > In a language like Java the interpreter would have noticed that x was
> > an undeclared variable and an error message would be issued. R, on the
> > other hand, allows the code to run, as x exists in the global
> > environment. I was trying to avoid such situations by setting the
> > environment of f to be NULL. If there is a better way to catch this
> > type of errors I would be interested in knowing about it.
>
> The codetools package. See http://www.stat.uiowa.edu/~luke/R/codetools.
>
> (They are not R errors, but the codetools package can tell you that 'x' is
> not necessarily in scope.)
Yep, and nor is "<-", "+", and "{"...
(Does codetools actually check those? Probably not, but it goes to
show why full encapsulation is hard or impossible to achieve in a
language where "everything", including functions and operators, are
objects.)
--
O__ ---- Peter Dalgaard Ă˜ster Farimagsgade 5, Entr.B
c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907
______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html