[Rd] Is it possible to generate an error when a function is reassigned?
Duncan Murdoch
murdoch.duncan at gmail.com
Sun May 23 23:35:41 CEST 2010
thmsfuller066 at gmail.com wrote:
> Hello All,
>
> length() is a function. If I accidentally assign something else to
> length, it will give me some unexpected results.
>
> Although the freedom to redefine any funcion maybe good in sometime,
> it may be unwanted in many other cases. Is there a way to forbidden
> such changes on functions?
>
>
You aren't changing length(), you're creating a new function with the
same name that hides the original one. The distinction is important
because packages with namespaces will not be affected by your error,
it's not really as serious as you might think.
The general advice to protect yourself from mistakes like this is to
limit how much of your code you keep in the global environment. Keep
your code in scripts and source them into an empty environment, or
(better) put your code into a package and use a namespace. If you make
a mistake and hide an important function you'll still cause local
problems, but you may find it easier to fix them.
Duncan Murdoch
More information about the R-devel
mailing list