[Rd] possible bug in formals

2010-05-23 Thread Josef Leydold
Hi,

I am a little bit surprised by the following output of
'formals'. Is this the intended behavior?

 f - function(a=1,b=-1) { a+b }
 class(formals(f)$a)
[1] numeric
 class(formals(f)$b)
[1] call


Josef


-- 


-
Josef Leydold   |  WU (Vienna University of Economics and Business)
|  Institute for Statistics and Mathematics
-
Augasse 2-6 |  Tel.   +43 1 31336 4695
A-1090 Vienna   |  FAX+43 1 31336 774
European Union  |  email  josef.leyd...@wu.ac.at

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] possible bug in formals

2010-05-23 Thread Uwe Ligges



On 23.05.2010 16:14, Josef Leydold wrote:

Hi,

I am a little bit surprised by the following output of
'formals'. Is this the intended behavior?


f- function(a=1,b=-1) { a+b }
class(formals(f)$a)

[1] numeric

class(formals(f)$b)

[1] call


Josef





Yes, the arguments have not yet been evaluated, hence -1 is still a 
language object.


Try to write
 f- function(a= +1, b= -1) { a+b }
and you will find that this is a fascinating feature.

Uwe Ligges

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


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

2010-05-23 Thread Duncan Murdoch

thmsfuller...@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

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel