thmsfuller066 at gmail.com
2010-May-23 20:36 UTC
[Rd] Is it possible to generate an error when a function is reassigned?
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? -- Tom
Duncan Murdoch
2010-May-23 21:35 UTC
[Rd] Is it possible to generate an error when a function is reassigned?
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