Re: [R] How to get the namespace of a function?

From: Peter Dalgaard <p.dalgaard_at_biostat.ku.dk>
Date: Sat 04 Feb 2006 - 00:08:25 EST

Prof Brian Ripley <ripley@stats.ox.ac.uk> 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
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)                  FAX: (+45) 35327907

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Sat Feb 04 00:36:05 2006

This archive was generated by hypermail 2.1.8 : Sat 04 Feb 2006 - 03:25:05 EST