From murdoch.duncan at gmail.com  Sun May 23 23:35:41 2010
From: murdoch.duncan at gmail.com (Duncan Murdoch)
Date: Sun, 23 May 2010 17:35:41 -0400
Subject: [Rd] Is it possible to generate an error when a function is
	reassigned?
In-Reply-To: <AANLkTimFWoNjpbgagyXiMkYpKUJ_-3KHRk-BZY7a7IYB@mail.gmail.com>
References: <AANLkTimFWoNjpbgagyXiMkYpKUJ_-3KHRk-BZY7a7IYB@mail.gmail.com>
Message-ID: <4BF99FAD.9050304@gmail.com>

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



[Rd] Is it possible to generate an error when a function is reassigned?
Duncan Murdoch